在 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。

更新於: 2019 年 7 月 30 日

137 次觀看

開啟你的 職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.