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 出阁宴祝福语姐姐简短