在 MongoDB 中比较多个属性?
要比较多个属性,请使用 $where 运算符。我们首先使用文档创建一个集合 -
> dbcomparingMultiplePropertiesDemoinsertOne({"Values":[10,70,60]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cf228fcb64a577be5a2bc0a")
}以下是使用 find() 方法显示集合中所有文档的查询 -
> dbcomparingMultiplePropertiesDemofind()pretty();
这将生成以下文档 -
{
"_id" : ObjectId("5cf228fcb64a577be5a2bc0a"),
"Values" : [
10,
70,
60
]
}用例 1:如果条件变为 true,那么您将获得一个数组,否则不会显示任何内容。以下是用于比较 MongoDB 中多个属性的查询。
> dbcomparingMultiplePropertiesDemofind({ $where : "thisValues[1] > thisValues[2]" });由于 70 > 60,这将生成以下文档 -
{ "_id" : ObjectId("5cf228fcb64a577be5a2bc0a"), "Values" : [ 10, 70, 60 ] }
用例 2:如果条件变为 false,那么没有任何内容会被显示。以下是用于比较 MongoDB 中多个属性的查询 -
> dbcomparingMultiplePropertiesDemofind({ $where : "thisValues[1] < thisValues[2]" });对于错误条件,不会显示数据,因为 70 < 60 为 false。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP