MongoDB - "$$hashKey 在 \'fieldName".$$hashKey\' 中以美元 ($) 为前缀的字段 \'$\' 对存储无效。



尝试更新文档时,我收到字段timesToDisplay的上述错误。

MongoDB版本2.6.7。

整个模型:

msg = {
          'name': '',
          'template': '',
          'displayDurInMilliSec': 0,
          'timesToDisplay': [],
          'images': [],
          'texts': [],
          'screen': []
       }

我想我会在其他 3 个数组字段上遇到同样的错误。

我尝试使用$set但遇到了同样的错误。

代码:

function updateMessage(msg) {
    var conditions = {_id: msg._id}
      , update = { 'name': msg.name,
                   'template': msg.template,
                   'displayDurInMilliSec': msg.displayDurInMilliSec,
                   'timesToDisplay': msg.timesToDisplay,
                   'images': msg.images,
                   'texts': msg.texts,
                   'screen': msg.screen
    }
    messageModel.update(conditions, update, callback);
    function callback(err, numAffected) {
        if (!err) console.log(numAffected)
        else console.log(err)
    }
}

编辑:msg参数本身就是一个文档:

{ _id: '557d58abd54955480db6694f',
  name: 'msg99',
  timesToDisplay: [ { startDate: '2015-06-19T21:00:00.000Z',
                   '$$hashKey': 'object:214',
                    endDate: '2015-06-25T21:00:00.000Z',
                    daysOfTheWeek: [Object],
                    startTimeOfDay: '11',
                    endTimeOfDay: '13' } ],
 images: [],
 texts: [],
 screen: [ 1 ],
 '$$hashKey': 'object:54',
 displayDurInMilliSec: '40189',
 template: 'templates/Template2.html' }

$$hashkey字段由 AngularJS 在使用 ngRepeat 或 ngOptions 时添加。对于 ngRepeat,您可以通过附加 track by $index 来更改重复字符串。要使用ngOptions,您必须自己过滤掉该字段。AngularJS提供了一个快速的解决方案来过滤它:angular.toJson。这将过滤掉所有以两个美元符号为前缀的字段。查看文档。

我意识到这不是MongoDB的答案,但这个特定的错误($$hashkey),通常是由于AngularJS。

最新更新