猫鼬如何将子文档填充到另一个集合中

问题描述 投票:1回答:1

组织者,产品集合,我需要将产品表与已发布的用户ID和组织者集合以及购买的用户ID再次与用户和组织者一起加入。我可以与用户一起加入产品,但不能与组织者一起加入用户。

Products.find({})
  .populate({
    path: 'intresteduserIds.userId',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'intresteduserIds.user_organizationid' } /* How to join with organizer collection which have organizer id*/
  })
 .populate('product_postedby');

产品架构:

const ProductsObj = new Schema({
  product_title: String,
  product_category: String, 
  product_startDate: Date,
  product_startTime: String,
  product_endDate: Date,
  product_endTime: String,
  product_isPrivateEvent: Boolean, 
  product_desc: String,
  product_postedby: { type: mongoose.Schema.ObjectId, ref: 'User' },
  intresteduserIds: [
    {
      userId: {
        type: mongoose.Schema.ObjectId,
        ref: 'User',
        requested: false
      },
      name: String,
      email: String,
      mobilenumber: String,
      notes: String,
      requirestedDate: Date,
      status: String, 
      eventsList: [
        {
          id: mongoose.Schema.ObjectId,
          name: String
        }
      ],
      intrestedType: String 
    }
  ]
});

包含组织者ID的用户架构:

const usersSchema = new Schema({
  user_organizationid: {
    type: mongoose.SchemaTypes.ObjectId,
    required: false,
    index: true,
    ref: 'Oranizations'
  },
  user_firstname: String,
  user_lastname: String,
  user_username: String,
  user_mobilenumber: String,
  user_mobilenumber_code: String,
  user_email: String,
  user_role: {
    type: mongoose.SchemaTypes.ObjectId,
    required: false,
    index: true
  }
});

组织者架构:

const organizerSchema = new Schema({
  org_name: String,
  org_type: String, // organizer,ground , ecommerce seller,
  org_category: String, // corporate company,supplier
  org_logo: String,
  org_address_id: { type: mongoose.Schema.ObjectId, required: false },
  org_stack_exchange_id: String,
  org_created_dated: Date,
  org_updated_date: Date,
  org_activte_status: Boolean,
  user_hashcode: String
});

所以最终我需要:

{
  product:'product info',
  posted_userid:{
  posted_userinfo:'',
  organizerInfo:'join with user if there this information'
  },
  intrestedusers:{
    userid:{
      userinfo:'',
      organizerInfo:'join with user if there this information'
    }
  }
}

如何在这里获得解决方案?

node.js mongodb mongoose mongoose-populate
1个回答
1
投票

内部路径必须是user_organizationid而不是intresteduserIds.user_organizationid

Products.find({})
  .populate({
    path: 'intresteduserIds.userId',
    populate: {path: 'user_organizationid' } 
  })
 .populate('product_postedby');
© www.soinside.com 2019 - 2024. All rights reserved.