1

I have the following schema called scenarios

var scenarios = new Schema({
    title: 'String',
    type: 'String',
    description:  'String',
    authorId:  'String',
    presentation: [presentations_schema],
    revision: 'String',
    createDate: 'String',
    updateDate: 'Date',
    active: 'Boolean',
    display:  'Boolean',
    video: [video_schema],
});

I'm trying to update a field in the presentations schema which is here

var scenario_presentations = new Schema({
    scenarioId:  'String',
    pageLocation:  'String',
    pageNumber: [Number],
    syncManifest:  [Number],
    caption:'String',
    createdDate:  'Date',
    updateDate:  'Date',
    active: 'Boolean',
    display: 'Boolean'
});

I' trying to run an update based off of the answer here Partial update of a subdocument with nodejs/mongoose

Here is my attempt

var scenarios = require('../models/scenarios').Scenarios;
exports.updateScenario = function (req,res){

    scenarios.update({_id: '52cd8f43c1e8ba5009000008', presentation_id: '52cd8f43c1e8ba5009000009'},
            {$set: { 'presentation.$.pageLocation': 'someUrl'}},
                function(err, numberAffected, rawResponse) {
            if (err) {
                console.log(err);
                console.log('error returned');
                res.send(500, { error: 'Failed insert'});
            }

            if (!numberAffected) {
                console.log(rawResponse);
                res.send(403, { error: 'No rows affected' });
            }
            res.send(200);
        });

I keep getting if (!numberAffected) = true here is the document I'm trying to update

{
  authorId: "Austin(ShouldBeAHash)",
  revision: "1",
  active: true,
  display: true,
  sortOrder: 0,
  title: "sdfg",
  description: "gfds",
  _id: ObjectId("52cd8f43c1e8ba5009000008"),
  bundleId: [],
  video: [
    {
      thumbnailLocation: "someUrl",
      videoLocation: "SomeUrl",
      _id: ObjectId("52cd8f43c1e8ba500900000a")
    }
  ],
  status: [],
  presentation: [
    {
      pageLocation: "",
      _id: ObjectId("52cd8f43c1e8ba5009000009"),
      syncManifest: [
        0
      ],
      pageNumber: [
        0
      ]
    }
  ],
  subcategories: [],
  categories: [],
  __v: 0
}
Community
  • 1
  • 1
Austin Davis
  • 3,516
  • 10
  • 27
  • 47

1 Answers1

2

In your update call's selector parameter, presentation_id should be 'presentation._id':

scenarios.update({
    _id: '52cd8f43c1e8ba5009000008', 
    'presentation._id': '52cd8f43c1e8ba5009000009 }, ...
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471