2016-08-25 39 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"); 
    } 
});