【问题标题】:Mongoose populate how to join populate sub document to another collection猫鼬填充如何将填充子文档加入另一个集合
【发布时间】:2020-01-08 22:59:59
【问题描述】:

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

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

Products.find({})
  .populate({
    path: 'intresteduserIds.userId',
    populate: {path: 'user_organizationid' } 
  })
 .populate('product_postedby');

【讨论】:

  • @jdileep 你如何为 Organizer 创建模型? mongoose.model(??)
  • const Oranizations = mongoose.model("Oranizations", OrganizerSchema);我需要改变什么吗?
  • @jdileep 不,您在 userSchema ref 中也使用了 Oranizations。但当然更好地在这两个地方使用Organizations 并使用正确的语法。
  • @jdileep 以及如何为用户创建模型?
  • 和这个一样 const User = mongoose.model('User', usersSchema)
猜你喜欢
  • 2017-05-07
  • 2021-02-11
  • 2021-10-07
  • 2016-08-17
  • 2021-04-24
  • 2016-05-16
  • 2020-04-30
  • 2015-07-13
  • 2016-10-10
相关资源
最近更新 更多