[英]How can I make a multiple options query with mongodb driver for nodejs?
[英]How can I make a multiple conditioned query on nosql
如何為給查詢“ if( (is_new: false || from:'user1') && 參與者: 'users1'” 進行無 sql 查詢,其中參與者是具有兩個元素參與者的數組:[ 0: user1, 1: user2 ]?
is_new: true
participants: Array 0: 5e3536e1b29cf92840171f3e
1: 5e3537a9b29cf92840171f3f
fromUserId: 5e3537a9b29cf92840171f3f
嘗試這個
db.collection.find({
$or: [{ is_new: false }, { fromUserId: "userId1" }],
participants: { $in: ["userId1"] }
})
你可以在這里測試Mongo Playground
注意:將
userId1
替換為您的真實 _id
希望能幫助到你
你快到了@vector,只需要做一個小改動
在這里,我為您更新查詢:
db.your_collection.find({
$and: [
{
$or: [
{
is_new: false
},
{
fromUserId: ObjectId("5e3536e1b29cf92840171f3e")
}
]
},
{
participants: {
$elemMatch: {
$eq: ObjectId("5e3536e1b29cf92840171f3e")
}
}
}
]
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.