I run my mongo shell script like this:
mongo --quiet myscript.js > /tmp/my.json
I use printjson
in myscript.js. mongodb printjson will output ObjectId to my.json
, like this:
"_id" : ObjectId("5444a932ca62bbcba14a1082")
I read some source code from mongo shell. printjson
will run this code for the ObjectId object.
> x._id.tojson
function (){
return this.toString();
}
after mongo version 2.2, ObjectId("507c7f79bcf86cd7994f6c0e").toString()
will return the following string:
ObjectId("507c7f79bcf86cd7994f6c0e")
It's not I want. I use ObjectId("507c7f79bcf86cd7994f6c0e").valueOf()
.
This will return the following string:
507c7f79bcf86cd7994f6c0e
finally, I add one line in myscript.js
:
ObjectId.prototype.toString = function() { return '"' + this.valueOf() + '"'; }
I solved my problem. but I don't like change the original behavior of the toString()
.
Is there any better solutions?