使用对象作为在MongoDB中查找值的键
要查找值,请find()
在MongoDB中使用。在此之下,使用点表示法。让我们创建一个包含文档的集合-
> db.demo361.insertOne({"details":{"FirstName":"Chris","LastName":"Brown"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e56a61f54a481fef8ec7a19")
}
> db.demo361.insertOne({"details":{"FirstName":"David","LastName":"Miller"}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e56a62854a481fef8ec7a1a")
}
> db.demo361.insertOne({"details":{"FirstName":"John","LastName":"Doe"}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e56a63654a481fef8ec7a1b")
}
在find()
方法的帮助下显示集合中的所有文档-
> db.demo361.find();
这将产生以下输出-
{ "_id" : ObjectId("5e56a61f54a481fef8ec7a19"), "details" : { "FirstName" : "Chris", "LastName" : "Brown" } }{ "_id" : ObjectId("5e56a62854a481fef8ec7a1a"), "details" : { "FirstName" : "David", "LastName" : "Miller" } }
{ "_id" : ObjectId("5e56a63654a481fef8ec7a1b"), "details" : { "FirstName" : "John", "LastName" : "Doe" } }
以下是使用对象作为查找值的键的查询-
> db.demo361.find({},{"details.LastName":1});
这将产生以下输出-
{ "_id" : ObjectId("5e56a61f54a481fef8ec7a19"), "details" : { "LastName" : "Brown" } }{ "_id" : ObjectId("5e56a62854a481fef8ec7a1a"), "details" : { "LastName" : "Miller" } }
{ "_id" : ObjectId("5e56a63654a481fef8ec7a1b"), "details" : { "LastName" : "Doe" } }
以上是 使用对象作为在MongoDB中查找值的键 的全部内容, 来源链接: utcz.com/z/321646.html