如何在MongoDB中返回没有objectId的集合的文档?
要返回不带objectId的集合的文档,请设置_id:0。首先让我们创建一个包含文档的集合-
> db.returnDocumentWithoutObjectId.insertOne({"Name":"Carol","Age":25});{
"acknowledged" : true,
"insertedId" : ObjectId("5ce8ba6c78f00858fb12e8fa")
}
> db.returnDocumentWithoutObjectId.insertOne({"Name":"Sam","Age":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ce8ba6d78f00858fb12e8fb")
}
> db.returnDocumentWithoutObjectId.insertOne({"Name":"John","Age":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ce8ba6f78f00858fb12e8fc")
}
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.returnDocumentWithoutObjectId.find();
这将产生以下输出-
{ "_id" : ObjectId("5ce8ba6c78f00858fb12e8fa"), "Name" : "Carol", "Age" : 25 }{ "_id" : ObjectId("5ce8ba6d78f00858fb12e8fb"), "Name" : "Sam", "Age" : 21 }
{ "_id" : ObjectId("5ce8ba6f78f00858fb12e8fc"), "Name" : "John", "Age" : 23 }
以下是查询以返回MongoDB中没有objectId的集合的文档-
> db.returnDocumentWithoutObjectId.find({},{_id:0});
这将产生以下输出-
{ "Name" : "Carol", "Age" : 25 }{ "Name" : "Sam", "Age" : 21 }
{ "Name" : "John", "Age" : 23 }
以上是 如何在MongoDB中返回没有objectId的集合的文档? 的全部内容, 来源链接: utcz.com/z/357608.html