MongoDB查询仅返回嵌入式文档?
仅返回嵌入式文档是不可能的。但是,它将返回集合中的所有文档。让我们首先实现以下查询以创建包含文档的集合
>db.queryToEmbeddedDocument.insertOne({"UserName":"Larry","PostDetails":[{"UserMessage":"Hello","UserLikes":8},{"UserMessage":"Hi","UserLikes":6},{"UserMessage":"Good Morning","UserLikes":12},{"UserMessage":"Awesome","UserLikes":4}]});{
"acknowledged" : true,
"insertedId" : ObjectId("5c988a9f330fd0aa0d2fe4bd")
}
以下是在find()
方法的帮助下显示集合中所有文档的查询
> db.queryToEmbeddedDocument.find().pretty();
这将产生以下输出
{"_id" : ObjectId("5c988a9f330fd0aa0d2fe4bd"),
"UserName" : "Larry",
"PostDetails" : [
{
"UserMessage" : "Hello",
"UserLikes" : 8
},
{
"UserMessage" : "Hi",
"UserLikes" : 6
},
{
"UserMessage" : "Good Morning",
"UserLikes" : 12
},
{
"UserMessage" : "Awesome",
"UserLikes" : 4
}
]
}
以下是查询以返回集合中的所有文档
> db.queryToEmbeddedDocument.find({"PostDetails.UserLikes": {$gte: 8}},{PostDetails:1}).pretty();
这将产生以下输出
{"_id" : ObjectId("5c988a9f330fd0aa0d2fe4bd"),
"PostDetails" : [
{
"UserMessage" : "Hello",
"UserLikes" : 8
},
{
"UserMessage" : "Hi",
"UserLikes" : 6
},
{
"UserMessage" : "Good Morning",
"UserLikes" : 12
},
{
"UserMessage" : "Awesome",
"UserLikes" : 4
}
]
}
查看上面的示例输出,我们将获取所有文档,而我们只需要其中“ UserLikes”大于或等于8的那些文档。
以上是 MongoDB查询仅返回嵌入式文档? 的全部内容, 来源链接: utcz.com/z/334962.html