【问题标题】:Mongo DB find() query for nested documentMongodb find() 查询嵌套文档
【发布时间】:2015-04-03 20:15:04
【问题描述】:

我是 MongoDB 的新手。我想根据嵌套文档的条件检索文档。 示例:

db.user.insert([
{name:"abc",uid:"abc.b",pass:"abc3",follows:500,posts:78},
{name:"xyz",uid:"xyz.r",pass:"xyz4",follows:600,posts:78},
{name:"pqr",uid:"pqr.s",pass:"pqr5",follows:600,posts:78,
      comments:[{
                  uid:"abc.b",msg:"great"},{uid:"xyz.r",msg:"awesome"}
               ]}
])

这里,如何根据相当于 RDBMS 的条件检索文档:

 select * where comments.user="abc";

通过它我可以让用户评论“abc”。

提前致谢。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    使用 dot notation 访问查询对象中嵌入的数组元素。例如,下面将在comments数组的uid字段上查询user集合中值为"abc.d"的文档:

    db.user.find({"comments.uid": "abc.b"});
    

    【讨论】:

      【解决方案2】:
      db.user.find({"comments.user"="abc"})
      

      【讨论】:

      • 这个是错误的。 '=' 与 MongoDB 中的条件查询无关
      猜你喜欢
      • 2021-02-25
      • 2016-08-16
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2017-04-10
      • 1970-01-01
      相关资源
      最近更新 更多