且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB-如何从另一个集合中找到未被文档引用的所有文档

更新时间:2023-02-14 10:07:28

很多选项:

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'.

...