简体   繁体   English

mongodb:如何获取包含嵌套字段的文档

[英]mongodb : how to get documents that contains nested field

How can i get documents that contains nested field f5=12? 如何获取包含嵌套字段f5 = 12的文档?

db.products.insert( { f: "card", f1: {f2 : {f3 : 15 } } } ) db.products.insert({f:“ card”,f1:{f2:{f3:15}}})

db.products.insert( { f: "card", f1: {f2 : {f4 : {f5 : 12} } } } ) db.products.insert({f:“ card”,f1:{f2:{f4:{f5:12}}}})

db.products.insert( { f: "card", f1: {f2 : {f3 : {f5 : 43} } } } ) db.products.insert({f:“ card”,f1:{f2:{f3:{f5:43}}}})

db.products.insert( { f: "card", f1: {f2 : {f4 : 98 } } } ) db.products.insert({f:“ card”,f1:{f2:{f4:98}}})

I've tried smth like this: 我试过这样的东西:

db.products.find($where : "this.content.indexOf('f5 : 12') != -1") db.products.find($ where:“ this.content.indexOf('f5:12')!= -1”)

db.products.find( {$elemMatch : {f5 : 12} }) db.products.find({$ elemMatch:{f5:12}})

But it doesn't work.. Does anyone has any ideas? 但这是行不通的。有人有任何想法吗?

You can use dot notation in your query to do this: 您可以在查询中使用点符号来执行此操作:

db.products.find({'f1.f2.f4.f5': 12})

If you want something more flexible where you don't have to specify each of the parent keys, you'd need to search the docs using a $where operator. 如果您希望在不需要指定每个父键的地方更加灵活,则需要使用$where运算符搜索文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM