要比较多个属性,请在MongoDB中使用$where。让我们创建一个包含文档的集合-
> db.demo223.insertOne({"Scores":[56,78]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4ca03d395bdc2134730") } > db.demo223.insertOne({"Scores":[88,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4d103d395bdc2134731") } > db.demo223.insertOne({"Scores":[98,79]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4d803d395bdc2134732") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo223.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3ee4ca03d395bdc2134730"), "Scores" : [ 56, 78 ] } { "_id" : ObjectId("5e3ee4d103d395bdc2134731"), "Scores" : [ 88, 45 ] } { "_id" : ObjectId("5e3ee4d803d395bdc2134732"), "Scores" : [ 98, 79 ] }
以下是比较MongoDB中多个属性的查询-
> db.demo223.find({ $where : "this.Scores[0] > this.Scores[1]" });
这将产生以下输出-
{ "_id" : ObjectId("5e3ee4d103d395bdc2134731"), "Scores" : [ 88, 45 ] } { "_id" : ObjectId("5e3ee4d803d395bdc2134732"), "Scores" : [ 98, 79 ] }