【问题标题】:What's incorrect about this mongoose partial update这个猫鼬部分更新有什么不正确的
【发布时间】:2014-01-09 04:13:09
【问题描述】:

我有以下称为场景的架构

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
}

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2020-11-13
      • 1970-01-01
      • 2012-03-28
      • 2016-05-24
      相关资源
      最近更新 更多