为此,请在MongoDB中设置regex find()
。让我们创建一个包含文档的集合-
> db.demo228.insertOne({"Subjects":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fa51f03d395bdc213473b") } > db.demo228.insertOne({"Subjects":["MongoDB","Java","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fa52c03d395bdc213473c") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo228.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3fa51f03d395bdc213473b"), "Subjects" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5e3fa52c03d395bdc213473c"), "Subjects" : [ "MongoDB", "Java", "MySQL" ] }
以下是查找文档的查询,其中文档中至少有一项不在另一个项中-
> db.demo228.find({ "Subjects": /^(?!MongoDB|Java)/ });
这将产生以下输出-
{ "_id" : ObjectId("5e3fa52c03d395bdc213473c"), "Subjects" : [ "MongoDB", "Java", "MySQL" ] }