MongoDB中有内置的JSON.parse吗? [英] Any build-in JSON.parse in MongoDB?
问题描述
是否有任何我可以将字符串转换为对象的Mongo(命令行)函数?例如JSON.parse还是类似的东西?
Is there any Mongo (command line) function that I can turn a string into object? e.g. JSON.parse or something like that?
db.sessions.update({},{'$ set':{'extra':JSON.parse(stringData)}}))
db.sessions.update({}, {'$set':{ 'extra':JSON.parse(stringData) }});
我的解决方案:
function my_extra() {
db.tempData.find().forEach( function(obj) {
obj.extra = db.eval(obj.myString);
db.tempData.save(obj);
} );
};
my_extra();
但是,我尝试这样做:db.tempData.update({},{'$ set':{'extra':db.eval(myString)}})); 但它不起作用..说未定义myString. 所以我用this.myString但都行不通.这就是为什么我必须使用该功能.
However, I try this: db.tempData.update({}, {'$set':{ 'extra':db.eval(myString) }}); but it doesn't work.. saying myString is not defined. so i use this.myString but doesn't work neither. that's why I have to use the function.
有没有办法在第二个参数中引用myString?
is there a way to reference myString in the second parameter?
推荐答案
版本2.1+的Mongo Shell包含JSON
实用程序对象:
The version 2.1+ Mongo shell includes a JSON
utility object:
- 从对象到JSON:
JSON.serialize(object)
- 从JSON到对象:
JSON.parse(string)
- 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
注意:在2.4+版本的Mongo Shell中,使用JSON.stringify()
代替JSON.serialize()
http://docs.mongodb.org/manual/release-notes/2.4-javascript/
Note: In version 2.4+ Mongo shell, use JSON.stringify()
instead of JSON.serialize()
http://docs.mongodb.org/manual/release-notes/2.4-javascript/
这篇关于MongoDB中有内置的JSON.parse吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!