簡體   English   中英

比較 MongoDB 中的兩個未排序集合

[英]Comparing two unsorted collections in MongoDB

我正在嘗試比較兩個集合中的大量文檔。 為了給你一個估計,我在兩個集合中的每個集合中都有大約 1300 個文檔。

我想在比較兩個集合后生成一個差異比較報告。 我不需要確切指出缺少什么或添加了哪些新內容,我只需要能夠確定兩個文檔之間實際上存在一些差異 是的,除了 Mongo 的ObjectId ("_id")之外,我確實有每個文檔的唯一標識符。

注意:我已經使用非規范化數據模型實現了數據庫,這意味着我已經嵌入了文檔(文檔中的文檔)。

您認為實施相同解決方案的最佳方法是什么?

預先感謝您的時間撒瑪利亞人!

您應該在您關心的所有字段上使用$lookup和 $eq。

db.collection1.aggregate([
   {
      $lookup:
         {
           from: "collection2",
           let: { unique_id: "$unique_id", field1: "$field", field2: "$field", ... },
           pipeline: [
              { $match:
                 { $expr:
                    { $and:
                       [
                         { $eq: [ "$unique_id_in_2",  "$$unique_id" ] }
                         { $eq: [ "$field_to_match",  "$$field1" ] },
                         { $eq: [ "$field_to_match.2",  "$$field2" ] }
                       ]
                    }
                 }
              },
           ],
           as: "matches"
         }
    },
   {
     $match: {
         'matches.0': {$exists: false}
      }
   }
])

** mongo 3.6+ 語法用於查找。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM