2016-07-30 14 views
0

我搜索狀態「請求」的所有文檔,但我想排除狀態「notInterested」的所有文檔。搜索MongoDB和相同的字段,我想排除一些結果

下面是代碼:

getRelationships(){ 
    return Relationships.find({ 
     'whoId': Meteor.userId(), 
     'status' : 'request', 
     'status' : {$ne : 'notInterested'} 
    }); 
} 

不是現在的工作,但我有沒有錯誤。

+0

不是很清楚你想在這裏做什麼;如果您查詢狀態爲「請求」的文檔,則mongo將返回具有該狀態的文檔,其中包括不具有「notInterested」狀態的文檔。也許你是指另一個領域? – chridam

回答

1

在我下面的示例集合中,有三個「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" 
} 
+0

非常感謝 –