Joe
Joe

Reputation: 1772

mongodb - collection update doesn't actually update

I'm having a tough time trying to figure out this issue.

I'm trying to issue a collection level update that essentially updates all matched records with a number such as:

# I actually am using PHP driver but using the console has the same result
db.my_collection.update( {si: '647522529'}, {$set: {cl:'2'}} );

The above should find all records that are si='647522529' and then update the cl='2'.

The problem is the above command doesn't give any kind of error or warning so it looks like it executed properly, but when I run a distinct command on the 'cl' key it is giving mixed results (2/1):

> db.runCommand( { distinct: 'my_collection', key: 'cl', query: { si: '647522529' } } )
{
        "values" : [
                "2",
                "1"
        ],
        "stats" : {
                "n" : 113,
                "nscanned" : 113,
                "nscannedObjects" : 113,
                "timems" : 1,
                "cursor" : "BtreeCursor si_1"
        },
        "ok" : 1
}

Am I doing something incorrectly? I've tried reindexing the database and that didn't have any effect either.

Any help would be MUCH appreciated!

Upvotes: 0

Views: 864

Answers (2)

rohit kotian
rohit kotian

Reputation: 130

db.my_collection.update(Criteria, Update, multi:true)

This should work!

Refer to: http://docs.mongodb.org/manual/tutorial/modify-documents/

Upvotes: 0

gview
gview

Reputation: 15361

By default a mongo update will update a single document. See: http://docs.mongodb.org/manual/tutorial/modify-documents/

You have to provide the optional:

{ multi: true }

Upvotes: 1

Related Questions