0

I have the below document in my collection

{ 
    "_id" : ObjectId("53732dc386d78e446c73fbb3"),
    "elig" : 18,
    "tags" : { 
        "6" : "49.6", 
        "9" : "657"
    }, 
    "book" : { 
        "type" : "100",
       "id" : "59598699"
    }
}

If you notice , the tags is key-value pair with the keys in numerical form (basically FIX messages, tag and value pair )

I need to update tags.6 from String ("49.6") to Float (49.6).

When I tried for updating the book.type from "100" to 100, it worked. Below worked.

db.coll.find().forEach(function(data) {
    db.coll.update(
        {_id:data._id},
        {$set:{"book.type":parseFloat(data.book.type) }}
    );
})

But for tags.6,:

db.coll.find().forEach(function(data) {
    db.coll.update(
        {_id:data._id},
        {$set:{"tags.6":data.tags.6 }}
    );
})

it is giving error

2014-05-15T08:23:40.161+0100 SyntaxError: Unexpected number

Can't I have numbers as the keys ?

Please let me know how to update.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317

1 Answers1

0

The issue is how you access data. Use data.tags["6"] instead of data.tags.6. I.e. such update would work:

db.coll.update(
          {_id:data._id},
          {$set:{"tags.6":data.tags["6"] }}
);
Andrei Beziazychnyi
  • 2,897
  • 2
  • 16
  • 11