使用Mongoose更新$ inc其他行为然后MongoDB更新

时间:2015-06-25 17:31:45

标签: node.js mongodb mongoose

我尝试使用mongoose更新文档但失败了。我可以直接在Mongo中成功执行的查询就像:

db.orders.update(
    { 
        orderId: 1014428,
        'delivery.items.id': '5585d77c714a90fe0fc2fcb4' 
    },
    {
        $inc: {
            "delivery.items.$.quantity" : 1
        }
    }
)

当我尝试使用mongoose运行以下更新命令时:

this.update(
        {
            orderId: this.orderId ,
            "delivery.items.id": product.id
        },
        {
            $inc: {
                "delivery.items.$.quantity" : 1
            }
        }, function (err, raw) {
            if (err) {
                console.log(err);
            }

            console.log('The raw response from Mongo was ', raw);
        }
    );

我看到以下错误:

{ [MongoError: cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})]
  name: 'MongoError',
  message: 'cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})',
  index: 0,
  code: 16837,
  errmsg: 'cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})' }
The raw response from Mongo was  { ok: 0, n: 0, nModified: 0 }

我尝试了很多东西。对此有何建议?

根据要求提供架构:

var Order = new Schema({
    orderId: Number,
    orderDate: String,
    customerName: String,
    state: Number,
    delivery: {
         items: {type: Array, default: []},
         state: { type: Number, default: 0 }
    }
});

2 个答案:

答案 0 :(得分:3)

TL; DR:在执行更高级的查询时,请使用您的模型 <hive xmlns="uri:oozie:hive-action:0.1"> <job-tracker>${jobTracker}</job-tracker> <name-node>${nameNode}</name-node> <configuration> <property> <name>mapred.job.queue.name</name> <value>${queueName}</value> </property> </configuration> <script>hiveScript.hql</script> </hive> <ok to="end" /> <error to="kill" /> </hive> 而不是实例Order

this

说明:

映射Orders.update( { orderId: this.orderId , "delivery.items.id": product.id }, { $inc: { "delivery.items.$.quantity" : 1 } }, function (err, raw) { if (err) { console.log(err); } console.log('The raw response from Mongo was ', raw); } ); Model.update()之间的差异。

使用模型,然后将使用Model.update()

Document.update()

将映射到:

Model.update(conditions, doc, options, callback)

使用实例时,而不是调用Document.update()

db.collection.update(query = conditions, update = doc, options)

将映射到以下内容:

Document.update(doc, options, callback)

答案 1 :(得分:0)

不知道这是否有帮助,但在this question中,O.P。与mongoose 3.6.15有类似的问题,并声称它已在3.8.22中解决

编辑:在链接的问题中,O.P。在mongodb上进行了以下工作

db.orchards.update(
({"orchardId": ObjectId("5391c137722b051908000000")},
{"trees" : { $elemMatch: {"name":"apple"}}}),
{ $push: { "trees.$.fruits": ObjectId("54c542c9d900000000001234") }})

但这不适用于猫鼬:

orchards.update(
({"orchardId": ObjectId.fromString(orchard.id)},
{"trees" : {$elemMatch: {"name": "apple"}}}),
{$push: {"trees.$.fruits": ObjectId("54c542c9d900000000001234") }},function(err, data){ ...

在评论中,他说问题已经解决,切换到mongoose 3.8.22