0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Diolor, also, look out for your collection names. You have 'db.col' and 'db.venue' in the query.
@DanielCoupal Thanks. I wanted to use col in my example and forgot to change the second one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.