在mongoDB中查找嵌套文档?

时间:2017-07-17 14:02:02

标签: mongodb

这是一个mongoDB结构:

-users

- 制品

---代码

**Collection**
user:
[
    {  
        _id : ID,
        username : "u1",
        articles:
        [
            {_id : ID,title:"",url: "",tags:["a", "b", "c"]},
            {_id : ID,title:"",url: "",tags:["a1", "b1", "c3"]},
            ...
        ]
    },
    {  
        _id : ID,
        username : "u2",
        articles:
        [
            {title:"",url: "",tags:["a3", "b3", "c3"]},
            {title:"",url: "",tags:["a4", "b4", "c3"]},
            ...
        ]
    }
]

如何获得所有文章" c3"所有用户的标签?

1 个答案:

答案 0 :(得分:0)

假设您的收藏包含的文档包含包含标签的文章:

db.collection.find({articles.tags: "c3"}, {articles: 1})