我的集合是集合
{
"_id" : ObjectId(""),
"from" : 1,
"to" : 2,
"message" : "Hello",
}
{
"_id" : ObjectId(""),
"from" : 2,
"to" : 1,
"message" : "Hi",
}我想执行如下的SQL查询
select * from collection where (from=1 and to=2) or (from=2 and to=1)在mongodb中,我正在使用
db.getCollection('collection').find({
$or:[{
$and:[
{"from": 1},
{"to": 2},
],
$and:[
{"from": 2},
{"to": 1},
]
}]
})但是它返回0条记录提取。
那么如何在mongodb中使用(和)或(和)
发布于 2020-09-10 05:10:57
您的语法略有偏离:
db.getCollection('collection').find({
$or:[{ $and:[{"from": 1}, {"to": 2}] },
{ $and:[{"from": 1}, {"to": 1}] }]
})$or数组中的每个项都需要处于闭包{...}中。
https://stackoverflow.com/questions/63823142
复制相似问题