I have a field in mongodb that's a string: {"field": "some text"}
. I want to convert them all into single-element arrays containing the string: {"field": ["some text"]}
.
I know I can just loop through all the documents, get the field, then update, but I'm wondering if there's a cleaner way.
You could do it in a Reduce function of map/reduce to keep all the processing in mongodb. Essentially you would use map/reduce to put the results into a new collection and then you could copy them back to the old collection (or delete old one and rename the new one). This has the advantage of keeping everything inside of mongo.
Update: Another option might be for you to use db.eval for this. db.eval
executes on the server so the updates would be done on the server without any traffic/latency.
I think the only other option is as you described - do it on the client by querying and updating each one.