mongodb - Mongoose 更新参数为空

查看:135
本文介绍了mongodb - Mongoose 更新参数为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

初始化 ' update = {'$inc': {"Prodects.$.price": 1}},'

但无法传入update 参数中, 结果为'$inc': {} ,

版本号

node v6.6.0 
"mongoose": "^4.7.5",
MongoDB shell version v3.4.0


app.js

#开启调试模式
var mongoose = require('mongoose').set('debug', true);;

router.js

router.get('/test', function(req, res, next){

var conditions = {"Prodects.id": "2"},
          update =  {'$inc': {"Prodects.$.price": 1}},
          options =  {upsert:true};
          
      CartModel.update(conditions, update, options, function(err) {
      });
});

终端显示第二个参数的数据为空, 但是声明的是一个对象

hotnode  node process restarted

{ '$inc': { 'Prodects.$.price': 1 } }
Mongoose: carts.update({ 'Prodects.id': '2' }, { '$inc': {}, '$setOnInsert': { __v: 0 } }, { upsert: true })

第二个参数 ---- '$inc': {} , 初始化 ' update = {'$inc': {"Prodects.$.price": 1}},'

以至于无法更新数据库中的数据

解决方案

Mongoose遇到的问题汇总

var conditions = {"Prodects.id": "2"},
          update =  {'$inc': {"Prodects.$.price": 1}},
          options =  {upsert:true};

      CartModel.update(conditions, update, options, function(err) {
      });
});

打印结果

{ '$inc': { 'Prodects.$.price': 1 } }
Mongoose: carts.update({ 'Prodects.id': '2' }, { '$inc': {}, '$setOnInsert': { __v: 0 } }, { upsert: true })

以下方式可以

 Model.collection.update(conditions, update, function(err, result){
          res.send(result)
        });

这篇关于mongodb - Mongoose 更新参数为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆