【问题标题】:$all not working in mongodb$all 在 mongodb 中不起作用
【发布时间】:2018-09-05 07:15:27
【问题描述】:

我的收藏中有两个文档

{ participants: [ '5ab8fcf6d8bfca2cc0aebb37', '5ab8fd15d8bfca2cc0aebb38' ],
    _id: 5ab9a5a0cb274a2064b65d1b,
    __v: 0 
},
{ participants: [ '5ab8fcf6d8bfca2cc0aebb37', '5ab8fcf6d8bfca2cc0aebb37' ],
    _id: 5ab9a5a7cb274a2064b65d1c,
    __v: 0
}

我有很多人喜欢

persons = [ 5ab8fcf6d8bfca2cc0aebb37, '5ab8fcf6d8bfca2cc0aebb37' ]

现在我正在尝试使用此查询查找包含类似于数组personsparticipants 字段的文档。

Participant.find({participants: {$all: persons}}).exec()
        .then(connected => {
            console.log(connected);
            // perform some stuff
});

它把我两个文件都作为输出。 不知道是什么问题。

提前感谢。

【问题讨论】:

    标签: javascript node.js mongodb mongodb-query


    【解决方案1】:

    您也可以使用$eq 运算符

        Participant.find( { participants: { $eq: persons } })
          .then(connected => {
                    console.log(connected);
                    // perform some stuff
        });
    

    了解更多https://docs.mongodb.com/manual/reference/operator/query/eq/

    【讨论】:

    【解决方案2】:

    我认为您想要的是 $setEquals 运算符。

    db.collection.find({ $expr: { $setEquals: [ persons, "$participants" ] } } )
    

    如果$expr 运算符在您运行的mongod 版本中不可用,您可以将$setEquals 运算符与$redact 运算符一起使用。

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 2016-05-31
      • 2012-02-03
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2021-06-18
      相关资源
      最近更新 更多