node.js - Mongoose填充如何将填充子文档加入到另一个集合

标签 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'
    }
  }
}

我怎样才能在这里找到解决方案?

最佳答案

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

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

关于node.js - Mongoose填充如何将填充子文档加入到另一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59629090/

相关文章:

mongodb - MongoDB 中的验证器不起作用

javascript - 唯一索引不适用于 Mongoose/MongoDB

mongodb - Upsert Document和/或添加子文档

node.js - 如何在nodejs中比较两个日期

javascript - 游戏的每回合超时逻辑

java - 将node.js文件打包到fatJar中

node.js - AWS Elastic Beanstalk Node.js 日志没有时间戳

node.js - 如何在另一个集合上使用某些条件搜索一个 Mongoose 集合?

mongodb - MongoDB 使用哪个端口?

node.js - 使用 "it"覆盖 mocha "yield"以支持 "suspend"