MongoDB find()函数可以显示避免使用_id吗?
是的,我们可以在MongoDB中使用以下语法避免_id:
db.yourCollectionName.find({},{ _id:0});
首先让我们创建一个包含文档的集合:
> db.excludeIdDemo.insertOne({"CustomerName":"Larry"});{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7f62c1a844af18acdffb9")
}
> db.excludeIdDemo.insertOne({"CustomerName":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7f6311a844af18acdffba")
}
> db.excludeIdDemo.insertOne({"CustomerName":"Mike"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7f6351a844af18acdffbb")
}
> db.excludeIdDemo.insertOne({"CustomerName":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7f6381a844af18acdffbc")
}
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.excludeIdDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd7f62c1a844af18acdffb9"), "CustomerName" : "Larry" }{ "_id" : ObjectId("5cd7f6311a844af18acdffba"), "CustomerName" : "Chris" }
{ "_id" : ObjectId("5cd7f6351a844af18acdffbb"), "CustomerName" : "Mike" }
{ "_id" : ObjectId("5cd7f6381a844af18acdffbc"), "CustomerName : "Bob" }
以下是排除_id的查询find()
-
> db.excludeIdDemo.find({},{ _id:0});
这将产生以下输出-
{ "CustomerName" : "Larry" }{ "CustomerName" : "Chris" }
{ "CustomerName" : "Mike" }
{ "CustomerName" : "Bob" }
以上是 MongoDB find()函数可以显示避免使用_id吗? 的全部内容, 来源链接: utcz.com/z/348976.html