Mongoose检索同一集合的数组字段中未被引用的所有文档。

huangapple go评论66阅读模式
英文:

Mongoose retrieve all documents that are not referenced in the same collection's array field

问题

我有以下的Post模式。这个帖子模式包含一个可能包含其他帖子作为回复的数组字段。我想要做的是检索所有在任何帖子的回复字段中都没有被引用的文档。基本上,获取所有不是原始帖子的回复的帖子。

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
    creator: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        validate: [mongoose.Types.ObjectId.isValid, 'Creator ID is invalid']
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        validate: [mongoose.Types.ObjectId.isValid, 'Owner ID is invalid']
    },
    content: {
        type: String,
        required: 'Content is required'
    },
    likes: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Like',
            validate: [mongoose.Types.ObjectId.isValid, 'Like ID is invalid']
        }
    ],
    replies: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post'
        }
    ]
}, {
    autoCreate: true,
    timestamps: true
});

const Post = mongoose.model('Post', schema);

module.exports = Post;

我尝试使用$lookup$match$nin,但无法使其正常工作。任何帮助将不胜感激。

英文:

I have the below Post schema. The post schema contains an array field that may contain other posts as replies. What I would like to do is retrieve all documents which are not referenced in any post's replies field. Basically get all posts that are not replies of an original post.

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
    creator: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        validate: [mongoose.Types.ObjectId.isValid, 'Creator ID is invalid']
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        validate: [mongoose.Types.ObjectId.isValid, 'Owner ID is invalid']
    },
    content: {
        type: String,
        required: 'Content is required'
    },
    likes: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Like',
            validate: [mongoose.Types.ObjectId.isValid, 'Like ID is invalid']
        }
    ],
    replies: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post'
        }
    ]
}, {
    autoCreate: true,
    timestamps: true
});

const Post = mongoose.model('Post', schema);

module.exports = Post;

I have tried using $lookup with $match & $nin but I cannot make it work properly. Any help would be greatly appreciated.

答案1

得分: 0

我认为更新模式以跟踪原始帖子或回复之一会更容易,然后您只需通过该布尔字段进行查询,而不是循环遍历一系列数组。

英文:

I think it'd be easier to just update the schema to track either a) whether it's an original post or b) whether it's a reply. Then you can just do a query by that Boolean field rather than looping through a series of arrays.

答案2

得分: 0

以下是翻译好的代码部分:

如果我没记错这似乎可以工作

let posts = await MODELS.Post.aggregate([
    {
        $lookup: {
            from: 'posts',
            localField: '_id',
            foreignField: 'replies',
            as: 'references'
        }
    },
    {
        '$match': {
            'references._id': {
                '$exists': false
            }
        }
    },
    {
        '$sort': {
            createdAt: -1
        }
    },
    {
        '$project': { 
            references: false
        }
    },
    {
        '$limit': 10
    }
]);

请注意,代码中的单引号(')可能会根据您的编程环境而有所不同,但在翻译中保持了原样。

英文:

IF I'm not mistaken this seems to work:

            let posts = await MODELS.Post.aggregate([
                {
                    $lookup: {
                        from: 'posts',
                        localField: '_id',
                        foreignField: 'replies',
                        as: 'references'
                    }
                },
                {
                    '$match': {
                        'references._id': {
                            '$exists': false
                        }
                    }
                },
                {
                    '$sort': {
                        createdAt: -1
                    }
                },
                {
                    '$project': { 
                        references: false
                    }
                },
                {
                    '$limit': 10
                }
            ]);

huangapple
  • 本文由 发表于 2023年6月2日 09:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定