【问题标题】:mongodb find only specified elementsmongodb 只查找指定的元素
【发布时间】:2013-08-21 23:40:07
【问题描述】:

我有这个架构

{
    cc: [
          { user_id: "1", hasSeen:true}
         ,{ user_id: "2", hasSeen:false}
         ,{ user_id: "3", hasSeen:false}

    ]
    ,conversation: [{
        user_id: "1",
        text: "message by 1, to 2and3"
    }]
}
,{
    cc: [
          { user_id: "1", hasSeen:true}
         ,{ user_id: "2", hasSeen:false}
         ,{ user_id: "3", hasSeen:false}
         ,{ user_id: "4", hasSeen:false}
    ]
    ,conversation: [{
        user_id: "1",
        text: "message by 1, to 2,3and4"
    }]
}

这就是我设置“收件箱”数据库的方式。 要查找有用户“1”的对话,我使用以下查询:

find({ cc:{$elemMatch:{ user_id:"1"}} })

它效果很好,但它给了我两个结果。

现在我想找到 only 用户 1、2 和 3 的元素。 如果我使用这个查询

find({ cc:{$elemMatch:{ user_id:"1", user_id:"2", user_id:"3" }} })

它给了我第二个元素,女巫还包括用户 4。(我不想得到它)。
我的意思是最后一个查询用作(sql)“or”运算符,我需要使用“and”运算符。

有什么想法吗?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    用另一种方式解决,计算元素!

    UserInbox.findOne( { 'cc.user_id': { $all: ["1","2","3"] }, cc:{ $size: 3 } } );
    

    希望能帮助别人:)

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2019-05-03
      相关资源
      最近更新 更多