How to add an object to a collection inside another collection in MongoDB using Node.js

I know how to add an object to a collection in MongoDB using Node.js, for example:

router.post('/addProduct', function (req, res) { Partner.findByIdAndUpdate({ _id: req.body.partnerId }, { $push: { "products": { name: req.body.dataProduct.name } } }, { safe: true }, function (err, response) { if (err) throw err; res.json(response); }); }); 

but what if the product has a different table? How can I just add an object there?

Let's say this is my diagram:

 var partnerSchema = new mongoose.Schema({ name: String, products: [ { name: String, campaignList: [ { name: String, type: String, startDate: Date, endDate: Date, paymentMethod: String, partnerPayout: Number, ourPayout: Number } ] }] }); 

The identifier in each partner and product by default ._id for example. partner._id and product._id . That is why not in the diagram above. However, I send them from FrontEnd to BackEnd as req.parameter - usually, but I wanted to say it for sure :)

+7
javascript mongodb
source share
3 answers

It’s best to bet on the definition of the scheme and model for the campaign yourself and add it to the partner by link using _id

 var partnerSchema = new mongoose.Schema({ name: String, products: [ { name: String, campaignList: [ { type : mongoose.Schema.Types.ObjectId, ref : 'campaignModel' } ] }] }); var campaignSchema = new mongoose.Schema({ name: String, type: String, startDate: Date, endDate: Date, paymentMethod: String, partnerPayout: Number, ourPayout: Number }); var campaignModel = mongoose.model('campaignModel', campaignSchema); var partnerModel = mongoose.model('partnerSchema', partnerSchema); 

It is good practice to look for a time when you are trying to use semi-complex socket data or objects with more than two or three keys and retrieve them in your own collection. This not only facilitates the search for these documents, but also facilitates their use in combination with other objects.

Be sure to call .populate() during the request so that MongoDB knows that you need to attach documents from other collections, otherwise you just get an ObjectId array.

+1
source share

First, match the position of the array of required products. You can confirm this by checking a simple find:

 Partner.find({_id: req.body.partnerId), 'products.name': req.body.dataProduct.name }, { 'products.$': 1}) 

Use the positional $ operator to move a new object to an array in the corresponding product element:

 Partner.update({_id: req.body.partnerId), 'products.name': req.body.dataProduct.name }, { $push: { 'products.$.campaignList': { name: 'new campaign' }}}) 

Link https://docs.mongodb.com/manual/reference/operator/update/positional/

0
source share

try the following:

 router.post('/addProduct', function (req, res) { Partner.findOneAndUpdate({ _id: req.body.partnerId }, { $push: { "products": { name: req.body.dataProduct.name, $push: {"campaignList": {name: req.body.name}} } } }, { safe: true }, function (err, response) { if (err) throw err; res.json(response); }); }); 

Hope this helps you.

-one
source share

All Articles