我有以下模式称为场景

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],
});


我正在尝试更新演示文稿架构中的字段

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


我试图根据Partial update of a subdocument with nodejs/mongoose答案运行更新

这是我的尝试

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);
        });


我不断收到if (!numberAffected) = true这是我要更新的文档

{
  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
}

最佳答案

update调用的选择器参数中,presentation_id应该为'presentation._id'

scenarios.update({
    _id: '52cd8f43c1e8ba5009000008',
    'presentation._id': '52cd8f43c1e8ba5009000009 }, ...

10-05 20:29
查看更多