I can not make populate using nesting. Here is an example of my models:

// Схема открытых кейсов const openedSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: 'user', required: [true, 'user is required'], index: true }, weapon: { type: Schema.Types.ObjectId, ref: 'cases.weapons', required: [true, 'weapon is required'], index: true }, sellPrice: { type: Number, default: null }, status: { type: Number, default: 0 } }, { timestamps: true }); const opened = mongoose.model('opened', openedSchema); // список сейсов const casesSchema = new Schema({ name: { type: String, unique: true, required: [true, 'name is required'] }, price: { type: Number, required: [true, 'price is required'] }, weapons: [ { weapon: { type: Schema.Types.ObjectId, ref: 'weapon', index: true } } ] }, { timestamps: false }); const cases = mongoose.model('cases', casesSchema); // схема для оружия const weaponSchema = new Schema({ name: { type: String, unique: true, required: [true, 'name is required'] }, price: { type: Number, required: [true, 'price is required'] }, autoship: { count: Number, status: Boolean, price: Number } }, { timestamps: false }); const weapon = mongoose.model('weapon', weaponSchema); 

What do the documents themselves look like

 // cases { "_id": { "$oid": "59653bcfa9ac622e1913e10c" }, "name": "test case #1", "price": 256, "weapons": [ { "weapon": { "$oid": "59653bcfa9ac622e1913e10b" }, "_id": { "$oid": "59653bcfa9ac622e1913e10d" } }, { "_id": { "$oid": "59653d3279aeda2fda9fb490" }, "weapon": { "$oid": "59653c5d069f562eb0ba4ef3" } }, { "_id": { "$oid": "59653d38ba04de2fdddc459f" }, "weapon": { "$oid": "59653c893a772e2ef7b65a29" } } ], "__v": 0 } // opened { "_id": { "$oid": "5965d134c8c95972a1a498f5" }, "updatedAt": { "$date": "2017-07-12T07:35:16.419Z" }, "createdAt": { "$date": "2017-07-12T07:35:16.419Z" }, "user": { "$oid": "5965d0d6ea9db872360db98b" }, "weapon": { "$oid": "59653bcfa9ac622e1913e10d" }, "status": 0, "sellPrice": null, "__v": 0 } // weapon { "_id": { "$oid": "59653bcfa9ac622e1913e10b" }, "name": "AWP | Fever Dream", "price": 300, "autoship": { "status": true, "price": 167, "count": 5 }, "__v": 0 } 

The cases model has an array for models _id which points to itself, and weapon._id for populate to the weapon model, in the opened model string weapon serves to populate to the array for cases.weapons models. So, I do this:

  opened.find() .populate('cases.weapons') .then(_opened => { console.log(_opened); }) .catch(err => { logger.error(err); }); 

but populate doesn't work.

    1 answer 1

    To solve the problem, you need to opened following collection in the collection:

     case: { type: Schema.Types.ObjectId, ref: 'cases', required: [true, 'case is required'], index: true }, 

    To know at the right moment which case was opened.
    And also to remake the key of the weapon in the collection opened and refer not to cases.weapons , but directly to the weapon :

      weapon: { type: Schema.Types.ObjectId, ref: 'weapon', required: [true, 'weapon is required'], index: true }, 

    Because the admin once wants to remove some object from the case and all the associations that went through this connection will fly to hell.
    This does not pretend to be an excellent solution (combining for mongodb is not good), but still has the right to life.