首先让我们创建一个包含文档的集合-
> db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[89,43,32,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e9f9b50a6c6dd317adb3") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[32,33,34,40]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea13b50a6c6dd317adb4") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[45,56,66,69]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea25b50a6c6dd317adb5") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[46,66,77,88]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea3cb50a6c6dd317adb6") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.arrayElementsNotGreaterThanDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd9e9f9b50a6c6dd317adb3"), "Scores" : [ 89, 43, 32, 45 ] } { "_id" : ObjectId("5cd9ea13b50a6c6dd317adb4"), "Scores" : [ 32, 33, 34, 40 ] } { "_id" : ObjectId("5cd9ea25b50a6c6dd317adb5"), "Scores" : [ 45, 56, 66, 69 ] } { "_id" : ObjectId("5cd9ea3cb50a6c6dd317adb6"), "Scores" : [ 46, 66, 77, 88 ] }
以下是所有数组项均小于指定条件的查询-
> db.arrayElementsNotGreaterThanDemo.find({Scores: {$not: {$gt:45}}});
这将产生以下输出-
{ "_id" : ObjectId("5cd9ea13b50a6c6dd317adb4"), "Scores" : [ 32, 33, 34, 40 ] }