简体   繁体   English

在 mongodb 中“加入”的嵌套查询

[英]Nested query to "join" in mongodb

I'm trying to fetch something like this, first populating content then inside content there is an array field called comments and then from comments i want to populate user .我试图获取这样的东西,首先填充content然后在content有一个名为comments的数组字段,然后从我想要填充的comments中填充user

Here is the schema这是架构

const contentScheme = new Schema({
    post_id:  { type: Number },
    post_type:  { type: String },
    likes_count:  { type: String },
    likes_by: [{type: Schema.Types.ObjectId, ref: 'User'}],
    comments_count:{ type: Number },
    comments:[{type: Schema.Types.ObjectId, ref: 'Comment'}],
},{timestamps : true});


const commentScheme = new Schema({
    created_by: { type: Schema.Types.ObjectId, ref: 'User' },
    post_id:  { type: String },
    post_type:  { type: String },
    comment:  { type: String },
    reply_to:  { type: Number },
    pinned:{ type: Number },
  
},{timestamps : true});

const userSchema = new Schema({
    email: { type: String, required: true,  index: { unique: true } },
    avatar: { type: String },
    token: { type: String },
    display_name: { type: String , required: true},
    last_updated: {
        type: Date,
        default: Date.now
      }

});

here is what i do so far这是我目前所做的

ContentModel.findOne({
                        post_id:args.post_id,post_type:args.post_type},(err,rslt)=>{
                        if(err){ 
                            reject(err);
                        }
                        else {
                           console.log(rslt)
                            resolve(rslt);
                        }
                    }).populate("comments").populate({ path: 'comments.created_by', model: UserModel })

from my query above here is the result从我上面的查询这里是结果

{
  _id: new ObjectId("618cdecbf1551c02355e4e6a"),
  post_id: 1,
  post_type: 'note',
  likes_count: '1',
  likes_by: [
    new ObjectId("6187bbe0e4d3a0aa98fd0cc6"),
    new ObjectId("6187bbe0e4d3a0aa98fd0cc6"),
    new ObjectId("6187bbe0e4d3a0aa98fd0cc6"),
    new ObjectId("6187bed3e4d3a0aa98fd0cc9")
  ],
  comments_count: 4,
  comments: [
    {
      _id: new ObjectId("618dced6ee441fca8e4e1659"),
      created_by: new ObjectId("6187bbe0e4d3a0aa98fd0cc6"),
      post_id: '1',
      post_type: 'note',
      comment: 'INI COMMENT',
      reply_to: null,
      pinned: 0,
      replies: [],
      createdAt: 2021-11-12T02:17:58.464Z,
      updatedAt: 2021-11-12T02:17:58.464Z,
      __v: 0
    },
    {
      _id: new ObjectId("618dd2281c5f1ddc6589728d"),
      created_by: new ObjectId("6187bed3e4d3a0aa98fd0cc9"),
      post_id: '1',
      post_type: 'note',
      comment: 'TEST COMMENT MEMBER',
      reply_to: null,
      pinned: 0,
      replies: [],
      createdAt: 2021-11-12T02:32:08.092Z,
      updatedAt: 2021-11-12T02:32:08.092Z,
      __v: 0
    }
  ],
  createdAt: 2021-11-11T09:13:47.412Z,
  updatedAt: 2021-11-12T02:32:08.212Z,
  __v: 0
}

As you can see at comments.created_by still showing object id.正如您在comments.created_by看到的,仍然显示对象 ID。 Instead of user object代替用户对象

How can i fix it ?我该如何解决?

join more than one deep level you can try加入多个深度级别,您可以尝试

ContentModel.findOne().populate({ path:'comments', populate: {
   path: 'created_by',
   model: 'UserModel'
}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM