通过ObjectId显示MongoDB记录?
首先让我们创建一个包含文档的集合-
> db.findByObjectIdDemo.insertOne({"ClientName":"Larry","ClientAge":23});{
"acknowledged" : true,
"insertedId" : ObjectId("5cd68cd657806ebf1256f11a")
}
> db.findByObjectIdDemo.insertOne({"ClientName":"Chris","ClientAge":26});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd68cdc57806ebf1256f11b")
}
> db.findByObjectIdDemo.insertOne({"ClientName":"David","ClientAge":38,"isMarried":true});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd68cf657806ebf1256f11c")
}
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.findByObjectIdDemo.find().pretty();
这将产生以下输出-
{"_id" : ObjectId("5cd68cd657806ebf1256f11a"),
"ClientName" : "Larry",
"ClientAge" : 23
}
{
"_id" : ObjectId("5cd68cdc57806ebf1256f11b"),
"ClientName" : "Chris",
"ClientAge" : 26
}
{
"_id" : ObjectId("5cd68cf657806ebf1256f11c"),
"ClientName" : "David",
"ClientAge" : 38,
"isMarried" : true
}
以下是通过ObjectId查找的查询-
> db.findByObjectIdDemo.find({_id: new ObjectId("5cd68cf657806ebf1256f11c")}).toArray(function(err, myDocument) { console.log(myDocument); });
这将产生以下输出-
[{
"_id" : ObjectId("5cd68cf657806ebf1256f11c"),
"ClientName" : "David",
"ClientAge" : 38,
"isMarried" : true
}
]
以上是 通过ObjectId显示MongoDB记录? 的全部内容, 来源链接: utcz.com/z/335287.html