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 :)
DiPix
source share