0

I'm using node.js and mongoose and I have a problem.
Here is my model that i'm using:

const mongoose = require('mongoose');

const { Schema } = mongoose;

const prodSchema = new Schema({
  name: String,
  rate: String,
  rate_number: String,
  type: String,
  offers: {
    netflix: {
      url: String,
    },
    amz: {
      url: String,
    },
  },
  updated_at: Date,
}, {
  collection: 'prod',
});

const ProdModel = mongoose.model('prod', prodSchema);

module.exports = ProdModel;

For exemple I would like to set the netflix url and after set the amz url.

ProdModel.updateOne({ name: currentName },{ offers: { amz: { url: movies[i].url } } });

But when i want for exemple to set the netflix url amz return an empty Object :/

greg-449
  • 109,219
  • 232
  • 102
  • 145
Louis
  • 3
  • 2

1 Answers1

0

You can use mongo $set operator to update nested properties without overriding other properties in the parent object.

ProdModel.updateOne({ name: currentName }, { $set: { "offers.netflix.url": movies[i].url } });
Dan Starns
  • 3,765
  • 1
  • 10
  • 28