Friday, 14 February 2020

Help for Mongoose: populating the refs in subdocument.

Here's the scenario. I have a number of Clients, and I have a bunch of Item that I sell to them. The rate varies according to the client. Furthermore, the items have several Batch Numbers associated with them.In order to save the details of the situation, here is my Mongoose schema:const clientSchema = new mongoose.Schema({ id: { type: String, required: true, unique: true }, address: { type: String, required: false }, rates: [rateSchema] }); Here is the Rate Schema:const rateSchema = new mongoose.Schema({ rate: { type: Number, required: true }, item_id: { type: String, ref: 'Item', required: true } }) And here is the Item Schema:const itemSchema = new mongoose.Schema({ _id: { type: String, required: true, }, hsnCode: { type: String, required: false }, batches: [itemBatchSchema] }); Now, in Mongoose, I am trying to retrieve a collection as follows:Provided a client ID, I want to retrieve all rates, items and batches related to that client.Client [batches] | | | | [Rates] --------- Item I want to retrieve this entire (highly-nested) data in one fell swoop. However, I can't seem to work with Populate. Any help is really appreciated.

Submitted February 14, 2020 at 10:33PM by Mycroft2046

No comments:

Post a Comment