处理从字符串到字符串数组的Monggose模式更改处理

时间:2016-12-08 13:23:48

标签: javascript mongodb mongoose mongoose-schema

我目前的文档是这样的

{
    "_id" : ObjectId("55ece69df332eb0000d34e12"),
    "parent" : "P1",
    "hierarchy" : {},
    "hidden" : false,
    "type" : "xyz",
    "name" : "Mike",
    "code" : "M110",
    "date" : ISODate("2015-09-07T01:21:33.965Z"),
    "__v" : 3,
    "job_id" : "ca50fdf0-6904-11e6-b9af-1b0ea5d7f792"
}

现在我希望将job_id替换为数组。所以我在模型模式中将字段类型更改为数组。现在,当我尝试使用一些更改来更新现有文档时。 保存之前的文档看起来像这样

{
        "_id" : ObjectId("55ece69df332eb0000d34e12"),
        "parent" : "P1",
        "hierarchy" : {},
        "hidden" : false,
        "type" : "xyz",
        "name" : "Mike",
        "code" : "M110",
        "date" : ISODate("2015-09-07T01:21:33.965Z"),
        "__v" : 3,
        "job_id" : ["ca50fdf0-6904-11e6-b9af-1b0ea5d7f792",
                    "f1f04a95-42fa-41e5-a2c6-89c52c9c63f2"
                    ]
    }

所以当我调用model.save()方法时,我收到以下错误:

{ [MongoError: The field 'job_id' must be an array but is of type String in document {_id: ObjectId('55ece69df332eb0000d34e12')}]
  name: 'MongoError',
  code: 16837,
  err: 'The field \'job_id\' must be an array but is of type String in document {_id: ObjectId(\'55ece69df332eb0000d34e12\')}' }

处理这个问题的最佳方法是什么?

0 个答案:

没有答案