Model.update:初始空数组未正确更新

时间:2014-02-12 21:49:26

标签: mongodb mongoose

我有一个包含许多可选属性的相当复杂的文档,所以我更喜欢更新整个文档。

但是,我遇到了一些初始为空的数组问题。

部分模型:

description: [{
    heading: String,
    text: String
}],
images: [{
    title: String,
    description: String,
    type: String,
    url: String
}],

初始值:

description: [{
    heading: 'some heading',
    text: 'some text'
}],
images: [],

将以下对象添加到图像中:

{
    description: "img text"
    title: "img title"
    type: "image/jpeg"
    url: "http://mybucket.amazonaws.com/images/img.jpg"
}

并使用:

进行更新
Paths.update(path, function(err, numberAffected, raw) {
    ...
}

更新 路径是:

{ 
    customer_id: '52e1fa1b853285b2ad828152',
    name: 'Whitehorse Historic Buildings',
    polyline:
       [ [ 60.720551, -135.055365 ],
         [ 60.720551, -135.055365 ],
         ...
         [ 60.72057, -135.055426 ],
         [ 60.720561, -135.055407 ] ],
    poi:
       [ { name: 'Donnenworth House',
           latitude: 60.72053,
           longitude: -135.0555,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'Roundhouse',
           latitude: 60.72161,
           longitude: -135.0509,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'White Pass & Yukon Route Building',
           latitude: 60.72015,
           longitude: -135.0499,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'Berrigan Cabins',
           latitude: 60.71848,
           longitude: -135.0501,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'Log Skyscrappers',
           latitude: 60.71804,
           longitude: -135.0525,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'Old Log Church',
           latitude: 60.71828,
           longitude: -135.0541,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'Taylor House',
           latitude: 60.71889,
           longitude: -135.058,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'McKinnon House',
           latitude: 60.71973,
           longitude: -135.0607,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] },
         { name: 'Captain Martin House',
           latitude: 60.72051,
           longitude: -135.0561,
           audios: [],
           videos: [],
           images: [],
           description: [],
           streetview: [Object] } ],
     audios: [],
     videos: [],
     images:
         [ { title: 'img title',
             description: 'img desc',
             type: 'image/jpeg',
             url: 'http://mybucket.s3.amazonaws.com/images/52e1fa1b853285b2ad828152-Whitehorse Historic Buildings-img.jpg' } ],
     description:
         [ { heading: 'Whitehorse Historic Buildings',
             text: 'A  historical walking tour offered by the Yukon Historical & Museums Association' } ] } 

结果:

"images" : [  "[object Object]" ],

我能够在描述数组中添加和更新对象,该数组最初不为空。

有什么我做错了,或者听起来像个臭虫?

1 个答案:

答案 0 :(得分:0)

经过几个小时的调试后,我发现Mongoose不喜欢模型架构中的关键词 type 。更改后,我的问题得到了解决。

据我所知,类型不是JavaScript中的保留关键字,我认为应该更好地记录Mongoose中的保留字。