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 } } ] } }热门推荐
1 送心的祝福语简短
10 毕业女孩祝福语简短独特
11 新年至客户祝福语简短
12 喜得孙子祝福语简短唯美
13 官宣祝福语简短霸气
14 集体生日祝福语简短的
15 孩子语音婚礼祝福语简短
16 成语祝福语精选大全简短
17 考进高中祝福语简短
18 送偶像生日祝福语简短