我的收藏中有两个文档
{ participants: [ '5ab8fcf6d8bfca2cc0aebb37', '5ab8fd15d8bfca2cc0aebb38' ],
_id: 5ab9a5a0cb274a2064b65d1b,
__v: 0
},
{ participants: [ '5ab8fcf6d8bfca2cc0aebb37', '5ab8fcf6d8bfca2cc0aebb37' ],
_id: 5ab9a5a7cb274a2064b65d1c,
__v: 0
}
我有很多人喜欢
persons = [ 5ab8fcf6d8bfca2cc0aebb37, '5ab8fcf6d8bfca2cc0aebb37' ]
现在,我正在尝试使用此查询查找包含类似于数组 persons
的 participants
字段的文档。
Participant.find({participants: {$all: persons}}).exec()
.then(connected => {
console.log(connected);
// perform some stuff
});
它把两个文档都作为输出抛给我。 我不知道是什么问题。
提前致谢。
最佳答案
我想你想要的是 $setEquals
运营商。
db.collection.find({ $expr: { $setEquals: [ persons, "$participants" ] } } )
您可以将 $setEquals
运算符与 $redact
一起使用运算符如果 $expr
运算符在您运行的 mongod
版本中不可用。
关于javascript - $all 不在 mongodb 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503396/