Reputation: 13450
Based on this SO question and actually this answer I try to update a field on a subdocument.
My code is:
db.venue.find().limit(10).forEach(
function (elem) {
for (var i = elem.events.length - 1; i >= 0; i--) {
elem.events[i].urls = [];
if(elem.events[i].facebook_id){
elem.events[i].urls = {'facebook_url' : elem.events[i].website }
}
if(elem.events[i].eventbrite_id){
elem.events[i].urls = {'eventbrite_url' : elem.events[i].website }
}
//delete elem.events[i].website
};
db.venue.update(elem);
}
);
If the elem.events[i]
has a facebook_id or eventbrite_id update respectively the urls which is an array (for schema reasons).
However I get a
Error: Printing Stack Trace
at printStackTrace (src/mongo/shell/utils.js:37:15)
at doassert (src/mongo/shell/assert.js:6:5)
at assert (src/mongo/shell/assert.js:14:5)
at DBCollection.update (src/mongo/shell/collection.js:220:5)
at (shell):13:18
at DBQuery.forEach (src/mongo/shell/query.js:264:9)
at (shell):1:27
Mon Jan 6 20:10:48.758 assert failed : need an object at src/mongo/shell/assert.js:7
Which I cannot really understand. Current user's privileges are fine.
Upvotes: 0
Views: 1156
Reputation: 19020
You need to call save
, not update
:
`db.venue.save(elem);`
Update takes two parameters: query document, and an update document.
The mistake is at the origin post you had linked.
Upvotes: 1