Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi update not working in MongoDB

Tags:

mongodb

I have a collection which I've populated from a CSV, however there are a few fields I don't need any more in the record, so I want to remove them, but it doesn't appear to be doing anything. This is from the CLI:

> db.stops.update({}, { $unset: {stop_lat: 1, stop_lon: 1} }, { multi: 1 })
> db.stops.findOne({stop_id: 1000})

{
  "_id" : ObjectId("50d30386884be8bf2a6c208a"),
  "stop_id" : 1000,
  // some other fields
  "stop_lat" : -27.339115,
  "stop_lon" : 153.043884,
}

What am I doing wrong here?

like image 832
nickf Avatar asked Dec 01 '22 20:12

nickf


1 Answers

Your syntax is not correct. The update operation in the CLI for versions before 2.2 looks as follows :

update(criteria, update, upsert, multiple)

When corrected it works :

> db.stops.update({}, { $unset: {stop_lat: 1, stop_lon: 1} }, false, true)
> db.stops.findOne({stop_id: 1000})
{ "_id" : ObjectId("50d30386884be8bf2a6c208a"), "stop_id" : 1000 }

EDIT : As Sammaye points out the syntax you're using is valid on the latest stable version of mongod.

like image 115
Remon van Vliet Avatar answered Dec 23 '22 05:12

Remon van Vliet