Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  112] [ 7]  / answers: 1 / hits: 19784  / 13 Years ago, tue, september 13, 2011, 12:00:00

I have a field in mongodb that's a string. {field: some text}, I want to convert them all into arrays. {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.



Thanks.


More From » mongodb

 Answers
50

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.


[#90122] Monday, September 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josh

Total Points: 391
Total Questions: 112
Total Answers: 90

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
;