[英]Comparing two unsorted collections in MongoDB
I am trying to compare a large number of documents in two collections.我正在尝试比较两个集合中的大量文档。 To give you an estimate, I have around 1300 documents in each of the two collections.
为了给你一个估计,我在两个集合中的每个集合中都有大约 1300 个文档。
I want to generate a diff comparison report after comparing the two collections.我想在比较两个集合后生成一个差异比较报告。 I do not need to point out exactly what is missing or what new content has been added, I just need to be able to identify that there is in fact some difference between the two documents .
我不需要确切指出缺少什么或添加了哪些新内容,我只需要能够确定两个文档之间实际上存在一些差异。 Yes, I do have a unique identifier for each documents other than Mongo's
ObjectId ("_id")
.是的,除了 Mongo 的
ObjectId ("_id")
之外,我确实有每个文档的唯一标识符。
Note: I have implemented the database using the denormalized data model, which means I have embedded documents (documents within documents).注意:我已经使用非规范化数据模型实现了数据库,这意味着我已经嵌入了文档(文档中的文档)。
What would you say is the best way to go about implementing a solution for the same?您认为实施相同解决方案的最佳方法是什么?
Thank you in advance for your time samaritans!预先感谢您的时间撒玛利亚人!
You should use $lookup and $eq on all the fields you care about.您应该在您关心的所有字段上使用$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+ syntax for lookup. ** mongo 3.6+ 语法用于查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.