Mongodb $设置不起作用

时间:2015-10-08 22:17:04

标签: node.js mongodb mongoose

我正在尝试更新嵌套文档。查询返回正确的文档,但该属性未更新。

型号:

ready

查询:

{
     _id: '560b072434b72aa4050fff9f',
     trips: [
       {
          tripId: '561581ef9387780e76469e96',
          startDate: "2015-11-17T06:00:00.000Z",
          endDate: "2015-11-18T06:00:00.000Z"
       },{
          tripId: '5617d1bb1d42c4da90d3bdea',
          startDate: "2015-10-17T06:00:00.000Z",
          endDate: "2015-10-18T06:00:00.000Z"
       }
     ],
}

架构:

UserData.update(
      { '_id': req.query._id, 'trips.tripId': req.query.tripId },
      { '$set': { 'trips.$.startDate' : req.query.newStartDate,
                  'trips.$.endDate' : req.query.newEndDate} },
      { 'multi': true },
      function(e, doc){
           console.log(doc);
      }
 );

1 个答案:

答案 0 :(得分:0)

@BlakesSeven引导我回答。我补充说:

var ObjectId = require('mongodb').ObjectID;

在我的app.route之前在server.js中,使用npm安装mongodb,然后更改

{ '_id': req.query._id, 'trips.tripId': req.query.tripId },

{ '_id': ObjectId(req.query._id), 'trips.tripId': ObjectId(req.query.tripId) },

现在一切都在更新。谢谢Blakes Seven!