我需要将条件传递给 find
方法。但是使用比较运算符 !=
。在 MongoDB 中有可能吗?我在 docs 中看到AND
、OR
、<
、>
等运算符,但否定运算符。您是否知道如何获取所有记录,即 recordType
不等于 Message
?
collection.find({'recordType' : 'Message'}).toArray(function(err, results) {
// <-- need NOT EQUAL OPERATOR
});
$ne
是解决方案。
Syntax: {field: {$ne: value} }
$ne
selects the documents where the value of the field is not equal (i.e. !=)
to the specified value. This includes documents that do not contain the field.
collection.find({'recordType' : {'$ne' : 'Message'}}).toArray(function(err, results)
{
console.log(results);
});
引用doc了解如何使用它。