2015-10-25 32 views
2

所以我想運行貓鼬查詢來查找所有searcharray標籤所在的帖子。匹配Mongoose中的數組中指定的所有值

標籤的數量不盡相同。

目前這返回任何標籤存在的職位。

Post.find({ 
     'tags.name': { 
      $in : searcharray 
     } 
    }, function(err, post) { 

     console.log(post); 

    } 
); 

我檢查了文檔,無法將這個文檔拼湊在一起。

感謝

回答

4

你想$all,這基本上是一個$and操作與更短的語法高達$in$or操作與更短的語法:

Post.find({"tags.name": { "$all": searcharray } }, function(err, posts) { 
     console.log(posts); 
}); 

這就需要你的「標籤」數組成員匹配searchArray列表中指定的「全部」項目的「名稱」。

作爲一個「或」條件,$in只是回憶至少包含其中一個項目的文檔,所以「和」條件表示所有項目。

+0

謝謝你明白了。 – poperob

0

您需要使用$all運算符。

Post.find({ 
     'tags.name': { 
      $all : searcharray 
     } 
    }, function(err, post) { 

     console.log(post); 

    } 
); 
相關問題