j dileep
j dileep

Reputation: 505

Mongoose populate how to join populate sub document to another collection

Organizers, Products collections, I need to join products table with posted user id and organizers collections and purchased userid with users and organizers again. I can join products with users but not able to join users with organizer.

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');

Products schema:

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 
    }
  ]
});

Users schema which contain organizer 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
  }
});

Organizer schema:

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
});

So eventually I need:

{
  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'
    }
  }
}

How can I get solution over here?

Upvotes: 1

Views: 1211

Answers (1)

SuleymanSah
SuleymanSah

Reputation: 17868

Inner path must be user_organizationid instead of intresteduserIds.user_organizationid

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

Upvotes: 2

Related Questions