如何使用MongoDB根据条件匹配字段值?
我尝试使用MongoDB根据if-else条件匹配相同的值,但该值不匹配如何使用MongoDB根据条件匹配字段值?,mongodb,mongoose,Mongodb,Mongoose,我尝试使用MongoDB根据if-else条件匹配相同的值,但该值不匹配 var abc = false; db.getCollection('deviceHistory').find({ $match: { $cond: { if: { $eq: [abc, false ] }, then: { 'purchaserId':
var abc = false;
db.getCollection('deviceHistory').find({
$match: {
$cond: {
if: {
$eq: [abc, false ]
},
then: {
'purchaserId':ObjectId("5d47bd2f005d3e192c3e82de")
},
else:{
'sellerId':ObjectId("5d47bd2f005d3e192c3e82de")
}
}
}
})
您可以使用运算符:
db.getCollection('deviceHistory').find({
$or : [
{ abc: false, purchaserId: ObjectId("5d47bd2f005d3e192c3e82de") },
{ abc: true, sellerId: ObjectId("5d47bd2f005d3e192c3e82de") }
]
})
您应该使用聚合框架。看看这里,如果你没有得到这些东西,你怎么能使用
$cond
。只需提供一些示例数据和预期输出。在你的Link 1字段和diff值中,但我需要$match中的1值和diff字段,这就是我编写此代码的原因好的,你能在你的问题中添加一些示例数据和预期输出吗。