I have a fixture that is prepopulating a set of polls the app uses. I’ve created some code to recognize whether the fixture is different than what is in mongo and updates mongo. It seems to working swimmingly for most records but every so often (seemingly randomly) inserts a bunch of nulls in an array field.
The fixture has the format:
qtext: 'What\'s your favorite candle scent?',
category: 'style',
thing: 'scented candle',
market: ['consumer'],
active: true,
image: 'candles.jpg',
choices: [
{ active: true, text: 'Lavender', votes: 0 },
{ active: true, text: 'Clean Cotton', votes: 0 },
{ active: true, text: 'Cinnamon', votes: 0 },
{ active: true, text: 'Apple', votes: 0 },
{ active: true, text: 'Vanilla', votes: 0 },
{ active: true, text: 'Other', votes: 0 },
{ active: true, text: 'Who cares about candles?', votes: 0 },
]
},
The update code to check on the choices field looks like this:
console.log("\t+checking existing choices");
for (var choice in corePolls[i].choices) {
if (storedPolls[i].choices[choice]) {
// do we need to change the text?
if (storedPolls[i].choices[choice].text != corePolls[i].choices[choice].text) {
console.log("\t\t*updating choice text to: " + corePolls[i].choices[choice].text)
Polls.update({_id: storedPolls[i]._id, "choices.text": storedPolls[i].choices[choice].text},
{$set: {"choices.$.text": corePolls[i].choices[choice].text}});
} else {
// check whether we inactivate the current choice. This assumes we wouldn't
// both change the name and change the active flag
if (storedPolls[i].choices[choice].active == undefined ||
(storedPolls[i].choices[choice].active &&
corePolls[i].choices[choice].active != storedPolls[i].choices[choice].active ) ) {
console.log("\t\t*updating choice " + storedPolls[i].choices[choice].text + " active flag to: " + corePolls[i].choices[choice].active)
Polls.update({_id: storedPolls[i]._id, "choices.text": storedPolls[i].choices[choice].text},
{$set: {"choices.$.active": corePolls[i].choices[choice].active}});
}
}
} else {
console.log("\t\t*inserting new choice: " + corePolls[i].choices[choice].text)
// we need to insert this new choice
Polls.update({_id: storedPolls[i]._id},
{$addToSet: {"choices": corePolls[i].choices[choice] } } );
} // insert new choice
And the weird inserts I’m getting (again, every once in a while) result in this:
"_id" : "St2D9qncYhvBL84eM",
"qtext" : "What is your favorite breakfast cereal?",
"category" : "breakfast",
"thing" : "breakfast cereal",
"market" : [
"consumer"
],
"active" : true,
"image" : "cereals.jpg",
"choices" : [
{
"active" : true,
"text" : "Captain Crunch",
"votes" : 0
},
{
"active" : true,
"text" : "Raisin Bran",
"votes" : 0
},
{
"active" : true,
"text" : "Cinnamon Toast Crunch",
"votes" : 0
},
{
"active" : true,
"text" : "Oatmeal",
"votes" : 0
},
{
"active" : true,
"text" : "Frosted Flakes",
"votes" : 0
},
{
"active" : true,
"text" : "Other",
"votes" : 0
},
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"votes" : 1
}
]
}
Thanks in advance for helping out.