MongoDB-查找另一个集合中未引用的所有文档

时间:2018-08-06 12:31:01

标签: mongodb aggregation-framework

这就是问题所在:

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

此查询将返回集合A中在集合B中没有引用的所有文档:

db.A.aggregate(
[
    {
        $lookup: {
            from: "B", <-- secondary collection name containing references to _id of 'A'
            localField: "_id",  <-- the _id field of the 'A' collection
            foreignField: "a_id", <-- the referencing field of the 'B' collection
            as: "references"
        }
    },
    {
        $match: {
            references: []
        }
    }
]);

我的问题是foreignField: "a_id"localField: "_id" .toString()。 如何在上述查询中添加toString()函数? 感谢您的帮助和评论。

0 个答案:

没有答案
相关问题