2016-08-25 37 views
0

如何從搜索結果中排除某些文檔?貓鼬 - 如何跳過查找特定文檔?

Stream.findOne({ 
    _id: query.id 
}, function(err, stream) { 

    var output = {}; 
    if (err) { 
     console.log("Error retrieving streams: " + err); 
    } 

    if (!stream) { 
     console.log("No stream found"); 
    } 

    if (stream) { 
     console.log("Stream found"); 
    } 
}); 

比如我想findOne跳過尋找的1234_id

可能嗎?

回答

1

,你可以嘗試做這個使用MongoDB Logical Query operators.

有關邏輯運算符的更多信息,請參閱MongoDB documentation for Logical Query Operators.

Stream.findOne({ $and : [ {_id: query.id } ,{ _id:{ $ne : "1234"} } ] 
}, function(err, stream) { 

    var output = {}; 
    if (err) { 
     console.log("Error retrieving streams: " + err); 
    } 

    if (!stream) { 
     console.log("No stream found"); 
    } 

    if (stream) { 
     console.log("Stream found"); 
    } 
});