这个猫鼬部分更新有什么不对

时间:2014-01-08 19:46:31

标签: node.js mongodb mongoose

我有以下架构称为方案

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
}

1 个答案:

答案 0 :(得分:1)

update来电的选择参数中,presentation_id应为'presentation._id'

scenarios.update({
    _id: '52cd8f43c1e8ba5009000008', 
    'presentation._id': '52cd8f43c1e8ba5009000009 }, ...
相关问题