比较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:如果条件变为假,则什么都不会显示以下是查询以比较MongoDB中的多个属性-

> dbcomparingMultiplePropertiesDemofind({ $where : "thisValues[1] < thisValues[2]" });

对于错误条件,由于70 <60为假,因此不会显示数据。