Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 使用Mongoose更新$inc其他行为然后MongoDB更新大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试使用mongoose更新文档,但它失败了.我可以直接在R_223_11845@ongo中成功执行的查询如下:
db.orders.update(
    { 
        orderId: 1014428,'delivery.items.id': '5585d77c714a90fe0fc2fcb4' 
    },{
        $inc: {
            "delivery.items.$.quantity" : 1
        }
    }
)
@H_616_4@当我尝试使用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);
        }
    );
@H_616_4@我看到以下错误

{ [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',@R_944_8798@ge: 'cAnnot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1,id: "5585d77c714a90fe0fc2fcb4" } ]})',index: 0,code: 16837,errmsg: 'cAnnot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1,id: "5585d77c714a90fe0fc2fcb4" } ]})' }
The raw response from Mongo was  { ok: 0,n: 0,nModified: 0 }
@H_616_4@我尝试了很多东西.有什么建议吗?

@H_616_4@根据要求,架构:

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

解决方法

TL; DR:在执行更高级的查询时,使用您的模型Order而不是实例:
orders.update(
    {
        orderId: this.orderId,"delivery.items.id": product.id
    },{
        $inc: {
            "delivery.items.$.quantity" : 1
        }
    },raw) {
        if (err) {
            console.log(err);
        }

        console.log('The raw response from Mongo was ',raw);
    }
);
@H_616_4@说明:

@H_616_4@映射Model.update()和Document.update()间的差异.

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

@H_452_2@model.update(conditions,doc,options,callBACk) @H_616_4@将映射到:

db.collection.update(query = conditions,update = doc,options)
@H_616_4@使用实例而不是调用Document.update()

Document.update(doc,callBACk)
@H_616_4@将映射到以下内容

db.collection.update(query = {_id: _iD},options)

大佬总结

以上是大佬教程为你收集整理的node.js – 使用Mongoose更新$inc其他行为然后MongoDB更新全部内容,希望文章能够帮你解决node.js – 使用Mongoose更新$inc其他行为然后MongoDB更新所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。