如何在MongoDB中比较多个属性?\n
要比较多个属性,请在 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 ] }
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP