MongoDB-如何从另一个集合中找到未被文档引用的所有文档 [英] MongoDB - how can I find all documents that aren't referenced by a document from another collection

查看:55
本文介绍了MongoDB-如何从另一个集合中找到未被文档引用的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是问题所在:

我在集合A中有文档,第一次创建它时,其他任何文档都没有引用它.在某个时候,将创建集合B中的文档,该文档将引用集合A中的文档的ObjectId.

I have document in collection A, when it is first created it is not referenced by any other documents. At some point a document in collection Bwill be created and it will reference the ObjectId of a document in collection A.

查找集合A中所有未被集合B中的文档引用的文档的最佳方法是什么?

What is the best way to find all Documents in Collection A that aren't referenced by I document in collection B?

我了解MongoDB不支持联接,但是我想知道是否有解决此问题的方法,除了从集合B中获取所有引用的ObjectId并在集合A中查找不在该列表中的文档之外,因为这种解决方案很可能无法很好地扩展.

I understand MongoDB doesn't support joins, but I wonder if there is a solution to this problem other than getting all referenced ObjectIds from Collection B and finding documents in collection A that aren't in that list, as this solution likely wouldn't scale well.

我可以仅将集合A中的文档嵌入到集合B中的文档中,然后将其从集合A中删除吗?那是最好的解决方案吗?

Can I just embed the document from Collection A into the document from collection B and then remove it from Collection A? Is that the best solution?

感谢您的帮助和评论.

推荐答案

很多选项:

1)将B文档的ID添加到A文档中的数组(反向引用).现在,您可以查找该数组中没有任何元素的A文档.问题:如果您有很多交叉引用,则数组可能对于文档大小而言太大.

1) Add the id of the B document to an array in the A document (a reverse reference). Now you can look for A documents that don't have any elements in that array. Issue: array may get too large for document size if you have lots of cross references.

2)添加一个集合C,该集合C跟踪A和B之间的引用.表现像联接表.

2) Add a collection C that tracks references between A's and B's. Behaves like a join table.

3)在引用"中有一个简单标志.当您添加B时,将其引用的所有A都标记为已引用".删除B时,请对B所引用的所有A进行B扫描,并取消标记不再具有引用的所有A.问题:可能不同步.

3) Have a simple flag in A 'referenced'. When you add a B mark all of the A's that it references as 'referenced'. When you remove a B, do a scan of B for all of the A's that are referenced by it and unflag any A's that no longer have a reference. Issue: could get out of sync.

4)在B上使用map reduce来创建一个集合,其中包含任何B引用的所有A的ID.使用该集合标记所有引用的A(先将它们全部取消标记).可以使用它来定期修复(3).

4) Use map reduce on B to create a collection containing the ids of all the A's that are referenced by any B. Use that collection to mark all the A's that are referenced (after unmarking all of them first). Can use this to fix (3) periodically.

5)将两种文档类型放在同一集合中,并使用map reduce发出_id和标记以表示在A中"或被B引用".在reduce步骤中,查找具有在A中"但未被"B引用"的任何组.

5) Put both document types in the same collection and use map reduce to emit the _id and a flag to say 'in A' or 'referenced by B'. In the reduce step look for any groups that have 'in A' but not 'referenced by B'.

...

这篇关于MongoDB-如何从另一个集合中找到未被文档引用的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆