Any built-in JSON.parse in MongoDB?

Is there any Mongo (command line) function so that I can turn a string into an object? like JSON.parse or something like that?

db.sessions.update ({}, {'$ set': {'extra': JSON.parse (stringData)}});


my decision:

function my_extra() { db.tempData.find().forEach( function(obj) { obj.extra = db.eval(obj.myString); db.tempData.save(obj); } ); }; my_extra(); 

However, I try this: db.tempData.update ({}, {'$ set': {'extra': db.eval (myString)}}); but this will not work .. saying that myString is undefined. so i use this.myString but not working. so I have to use this function.

Is there a way to refer to myString in the second parameter?

+4
source share
2 answers

The Mongo version 2.1+ shell has a JSON utility object:

  • From object to JSON: JSON.serialize(object)
  • From JSON to object: JSON.parse(string)

http://api.mongodb.org/java/2.6/com/mongodb/util/JSON.html

Note. In Mongo 2.4+ shell, use JSON.stringify() instead of JSON.serialize()
http://docs.mongodb.org/manual/release-notes/2.4-javascript/

+7
source

You can try the eval function:

 obj = eval("(function() { return {\"key\": \"value\"} })()") 

But note that this is unsafe because it can execute arbitrary Javascript code, including db.dropDatabase() .

+5
source

All Articles