要查找不符合条件的记录,请使用$ne。让我们创建一个包含文档的集合-
> db.demo148.insertOne({"Message":"Hello"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb37fdf09dd6d08539c0") } > db.demo148.insertOne({"Message":"Good"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb3efdf09dd6d08539c1") } > db.demo148.insertOne({"Message":"Bye"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb42fdf09dd6d08539c2") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo148.find();
这将产生以下输出-
{ "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" } { "_id" : ObjectId("5e32fb3efdf09dd6d08539c1"), "Message" : "Good" } { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" }
以下是在MongoDB中查找与条件不匹配的记录的查询-
> db.demo148.find({'Message' : {'$ne' : 'Good'}}).toArray(function(err, output) ... { ... console.log(output); ... });
这将产生以下输出-
[ { "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" }, { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" } ]