MongoDB 聚合查找不适用于 localfield _id

时间:2021-01-19 11:41:12

标签: mongodb lookup aggregation mongoose-schema

这是我的内容 Schema -

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ContentSchema = new Schema({
    linkName: {
        type: String,

    },
    noteUrl: {
        type: String,
    },
    url: {
        type: String,
    },
    description: {
        type: String,
    }
},
{
    timestamps: true
});
module.exports = mongoose.model('Content', ContentSchema);

和内容评论Schema -

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const contentCommentSchema = new Schema({
    text: {
        type: String,

    },
    user: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    content: {
        type: Schema.Types.ObjectId,
        ref: 'Content'
    }
},
{
    timestamps: true

});
module.exports = mongoose.model('contentComment', contentCommentSchema);

当我尝试从 contentComment 中 $lookup 时,它每次都返回 0 条记录。但是在 db 中为相应的内容创建了注释。这是 Aggregation -

const contentPromise = Content.aggregate([
    {
        $lookup: {
            from: "contentComments",
            localField: "_id",
            foreignField: "content",
            as: "comments"
        }
    }]);

我在那里遗漏了什么吗?

0 个答案:

没有答案