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

时间:2020-01-07 13:09:33

标签: node.js mongodb mongoose mongoose-populate

组织者,产品集合,我需要将产品表与已发布的用户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'
    }
  }
}

我该如何解决?

0 个答案:

没有答案