LearnBoost/mongoose — Issue #594: Dot notation in update causes spare array
Author: xjamundx
Created: 2011-11-04 20:34:55+00:00
State: open

var update= { '$set': {
'segmentThumbnails.1440': 1481,
segmentThumbnailsModified: new Date()
} }

Track.update({programId: programId}, update)

Converts it into a crazy sparse array:

db.tracks.findOne({_id: new ObjectId("4e986bda45e8c9f49e0d6a93")})
{
"_id" : ObjectId("4e986bda45e8c9f49e0d6a93"),
"airDate" : "2011-10-11",
"checkins" : [ ],
"created" : ISODate("2011-09-21T09:00:08.067Z"),
"duration" : 121,
"episode" : "9",
"hosted" : true,
"oldId" : "c66f0f0f-ce90-4fdc-94e4-deccc8920a2d",
"posts" : 151,
"programId" : "EP007473660255",
"season" : "13",
"segmentThumbnails" : [
null,
null,
null,
null,
null,
null,
null,
null,
null,
// ...
1481
]
}

Doing it like this it works fine and gives me an object as expected:

Track.collection.update({programId: programId}, update)

The correct result looks something like this:

> db.tracks.findOne({programId:"EP008466780044"})
{
"_id" : ObjectId("4eb449dd33bcf34e8b000003"),
"airDate" : "2011-11-04",
"checkins" : [ ],
"created" : ISODate("2011-11-04T20:23:57.223Z"),
"description" : "George goes under cover and follows the Man With the Yellow Hat; George goes on a wild chase after a squirrel steals his golf ball.",
"duration" : 30,
"episode" : "14",
"episodeTitle" : "Curious George, Spy Monkey; Castle Keep",
"hosted" : false,
"popular" : false,
"posts" : 0,
"programId" : "EP008466780044",
"season" : "2",
"segmentThumbnails" : {
"1440" : 1481
},
"segmentThumbnailsModified" : ISODate("2011-11-04T20:29:46.879Z"),
"showcardId" : "186074",
"slug" : "curious-george",
"startTimes" : {
"AKDT" : ISODate("2011-11-04T18:00:00Z"),
"AKST" : ISODate("2011-11-04T18:00:00Z"),
"CDT" : ISODate("2011-11-04T18:00:00Z"),
"CST" : ISODate("2011-11-04T18:00:00Z"),
"EDT" : ISODate("2011-11-04T17:00:00Z"),
"EST" : ISODate("2011-11-04T17:00:00Z"),
"HADT" : ISODate("2011-11-04T18:00:00Z"),
"HAST" : ISODate("2011-11-04T18:00:00Z"),
"MDT" : ISODate("2011-11-04T18:00:00Z"),
"MST" : ISODate("2011-11-04T18:00:00Z"),
"PDT" : ISODate("2011-11-04T18:00:00Z"),
"PST" : ISODate("2011-11-04T18:00:00Z")
},
"title" : "Curious George"
}

A quick search for "sparse array" didn't show me any matches, but I assume it's a known issue.

FYI, my model looks like this:

var Track = module.exports = new Schema({
airDate: String,
created: { type: Date, default: Date.now },
description: String,
duration: { type: Number, default: 30 },
episode: String,
episodeTitle: String,
hosted: { type: Boolean, default: false },
popular: { type: Boolean, default: false },
programId: { type: String, index: true },
season: String,
showcardId: String,
slug: String,
start: Date,
startTimes: {MST: {type: Date, default: null}
, MDT: {type: Date, default: null}
, EST: {type: Date, default: null}
, EDT: {type: Date, default: null}
, CST: {type: Date, default: null}
, CDT: {type: Date, default: null}
, PST: {type: Date, default: null}
, PDT: {type: Date, default: null}
, AKDT: {type: Date, default: null}
, AKST: {type: Date, default: null}
, HADT: {type: Date, default: null}
, HAST: {type: Date, default: null}
},
title: {type: String, set: setSlug},
checkins: [],
host: {},
posts: { type: Number, default: 0},
segmentThumbnails: {},
segmentThumbnailsModified: { type: Date, default: Date.now, index: true }
})