MongoDB如何在嵌套文档中设置条件?
假设我们需要查找一个值大于特定值的文档。为此,请在嵌套文档中使用点表示法,并使用$gt设置条件。
让我们看一个示例并创建包含文档的集合-
> db.demo688.insert(
... {
... information:{id:1,details:[
... {otherDetails:{
... values:75
... }
... }
... ]
... }
... }
... )
WriteResult({ "nInserted" : 1 })
> db.demo688.insert({
... information:
... {
... id:2,
... details:
... [
... {otherDetails:{
... values:78
... }
... }
... ]
... }
... }
... )
WriteResult({ "nInserted" : 1 })在find()方法的帮助下显示集合中的所有文档-
> db.demo688.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea57986a7e81adc6a0b3965"), "information" : { "id" : 1, "details" : [ { "otherDetails" : { "values" : 75 } } ] } }
{ "_id" : ObjectId("5ea5799ca7e81adc6a0b3966"), "information" : { "id" : 2, "details" : [ { "otherDetails" : { "values" : 78 } } ] } }以下是访问MongoDB嵌套文档的查询-
> db.demo688.find({"information.details.otherDetails.values":{$gt:75}});这将产生以下输出-
{ "_id" : ObjectId("5ea5799ca7e81adc6a0b3966"), "information" : { "id" : 2, "details" : [ { "otherDetails" : { "values" : 78 } } ] } }热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短