1

I've got a little problem with mongodb update method. This is my (simplified) schemas:

{
   profile: {
      birthdate: "XXX",
      city: "xxx"
   },
   account: {
      username: "...",
      password: "....",
      visits: 0
   }
}

I've got 2000 objects in DB. I want to anonymize datas by removing username values.

I tried this:

db.users.update({}, {$set: account: {username: ""}}, false, true);

That doesn't works, this query remove password field and visits. I understand why, but how to do ?

db.users.update({}, {account: { $set: {username: ""}}}, false, true);

mongo want to assign "$set" field, so that dosen't work.

My question is: how to update descendant fields without removing the entire "account" subdocument ?

(Setting "upsert" to true does the same.)

Metal3d
  • 2,905
  • 1
  • 23
  • 29

1 Answers1

4

You should use:

$set:{"account.username":""}

Just wondering why you don't use $unset?

Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • Thanks, but that doesn't work > db.users.update({}, { $set: { account.username : "" } }, false, true) Fri Mar 1 11:52:21 compile error: (shell):1 SyntaxError: Unexpected token . I should keep username because this database will be shared with other people that need to know that this field will exist. But I must hide existing username (I will put some random data) – Metal3d Mar 01 '13 at 10:54
  • My god... ok... I forget to use double quoted ! Thanks a lot ! – Metal3d Mar 01 '13 at 10:55