为了编写等式,您可以简单地将find()与匹配值一起使用。让我们创建一个包含文档的集合-
> db.demo145.insertOne({"ListOfNames":["Chris","David","Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f37bfdf09dd6d08539bb") } > db.demo145.insertOne({"ListOfNames":["Bob","John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f384fdf09dd6d08539bc") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo145.find();
这将产生以下输出-
{ "_id" : ObjectId("5e32f37bfdf09dd6d08539bb"), "ListOfNames" : [ "Chris", "David", "Mike" ] } { "_id" : ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames" : [ "Bob", "John" ] }
以下是实现MongoDB运算符$eq的查询-
> db.demo145.find({"ListOfNames":"John"});
这将产生以下输出-
{ "_id" : ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames" : [ "Bob", "John" ] }