mongodb - Mongoose 更新参数为空

 mobiledu2502872883 发布于 2022-10-26 05:12

初始化 ' 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}},'

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

2 个回答
  • Mongoose的文档写的有些太过简洁,所以有很多使用中的问题。

    希望大家一起努力,整理出一些Mongoose好的中文文档出来。

    Love MongoDB! Have Fun!

    2022-10-27 00:32 回答
  • 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)
            });
    2022-10-27 00:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有