在我下面的示例集合中,有三個「whoId」等於123的文檔。其中兩個文檔的狀態爲「請求」,另一個文檔的「notInterested」。該查詢具有包括特定狀態(即請求)和排除狀態(即notInterested)的條件。
$和運算符可以用來實現這一點。如果我正確理解你的問題,這個解決方案應該能解決你的問題。
我的人際關係收集數據: -
/* 1 */
{
"_id" : ObjectId("579e0d1d681b1cf15a897776"),
"whoId" : "123",
"status" : "request"
}
/* 2 */
{
"_id" : ObjectId("579e0d2f681b1cf15a897777"),
"whoId" : "2321111",
"status" : "notInterested"
}
/* 3 */
{
"_id" : ObjectId("579e0d5d681b1cf15a897778"),
"whoId" : "123",
"status" : "request"
}
/* 4 */
{
"_id" : ObjectId("579e0d65681b1cf15a897779"),
"whoId" : "123",
"status" : "notInterested"
}
查詢: -
db.Relationships.find({whoId : '123', $and : [{status : 'request'}, {status : {$ne : 'notInterested'}}]})
結果: - 請注意狀態 'notInterested' 沒有被選中。
/* 1 */
{
"_id" : ObjectId("579e0d1d681b1cf15a897776"),
"whoId" : "123",
"status" : "request"
}
/* 2 */
{
"_id" : ObjectId("579e0d5d681b1cf15a897778"),
"whoId" : "123",
"status" : "request"
}
不是很清楚你想在這裏做什麼;如果您查詢狀態爲「請求」的文檔,則mongo將返回具有該狀態的文檔,其中包括不具有「notInterested」狀態的文檔。也許你是指另一個領域? – chridam