Mongoose更新查询

时间:2018-05-08 03:06:41

标签: mongodb mongoose

Hello All我正在尝试使用mongoose进行更新,但我似乎无法使其正常运行。这是我正在保存的数据。

 var mongoose = require('mongoose');

var DataSchema = new mongoose.Schema({
  advisorEmail: {
    type: String
  },
  data: {
    type: Array
  },
  client: {
    type: String,
    required: true,
    trim: true
  },
  accountBalance: {
    type: Number,
    required: true,
    trim: true
  },
  description: {
    type: String,
    required: true,
    trim: true
  },
  AccountValue: {
    type: Number,
    required: true,
    trim: true
  },
  moneyMarket: {
    type: Number,
    required: true,
    trim: true
  },
  buyingPower: {
    type: Number,
    required: true,
    trim: true
  },
  netBalance: {
    type: Number,
    required: true,
    trim: true
  },
  advisorPercentage: {
    type: Number,
    required: true,
    trim: true
  }
});

var Data = mongoose.model('Table', DataSchema);
module.exports = Data;

下面是我正在编写的能够更新数据数组的代码

Data.update({
"advisorEmail":"Travis@travis.com"
}, 
{"$push":
    { "data":{"client": "Tester", "accountBalance": 21342,     
"description": "test From Nodejs", "AccountValue": 123234,     
"moneyMarket": 11000, "buyingPower": 01, "netBalance": 10, 
"advisorPercentage": 24}}}, function(err, updateData){
       if (err) throw (err);
       console.log(updateData);
    });

没有错误,这是我从" updateData返回的console.log。"

{ ok: 0, n: 0, nModified: 0 }

但是当我采用相同的结构并进入我当地的mongodb并做

 db.tables.update({
     "advisorEmail": "Travis@travis.com"
 }, {
     "$push": {
         "data": {
             "client": "bobby",
             "accountBalance": 123,
             "description": "TravisPUTO",
             "AccountValue": 123,
             "moneyMarket": 1000,
             "buyingPower": 0,
             "netBalance": 0,
             "advisorPercentage": 1
         }
     }
 })

它完全正常并且说这个..

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

这是一个猫鼬问题吗?我不确定是什么错。

感谢您提供帮助我的任何事情!

1 个答案:

答案 0 :(得分:1)

请使用其他字段更新您的架构。

advisorEmail : {type: String},
data: { type: Array },

所以mongoose确定了你更新的字段。