I have a simple stored script in system.js collection called getAllNotes.
script code: function (x) { return db.notes.find(); }
notes is a collection holding the data I wish to extract.
eval('getAllNotes()')
- works well and returns the data.
db.eval('getAllNotes()')
- returns a JSON string with various configuration that has nothing to do
with my collection See below. Any idea? anyone understands the difference between executing eval and db.eval?
"_mongo" : {
"slaveOk" : false,
"host" : "EMBEDDED"
},
"_db" : {
"_mongo" : {
"slaveOk" : false,
"host" : "EMBEDDED"
},
"_name" : "test"
},
"_collection" : {
"_mongo" : {
"slaveOk" : false,
"host" : "EMBEDDED"
},
"_db" : {
"_mongo" : {
"slaveOk" : false,
"host" : "EMBEDDED"
},
"_name" : "test"
},
"_shortName" : "notes",
"_fullName" : "test.notes"
},
"_ns" : "test.notes",
"_query" : {
},
"_fields" : null,
"_limit" : 0,
"_skip" : 0,
"_batchSize" : 0,
"_options" : 0,
"_cursor" : null,
"_numReturned" : 0,
"_special" : false
eval
takes a string of code that it executes.
db.eval
, according to the docs, takes a function to call and a list of parameters to pass to that function. In other words, leave off the single quotes '
here:
db.eval(getAllNotes())
As for the difference between the two, the docs also state it:
The helper
db.eval()
in the mongo shell wraps theeval
command. Therefore, the helper method shares the characteristics and behavior of the underlying command with one exception:db.eval()
method does not support thenolock
option.
Docs. Important stuff to learn things from before asking other people to do it for you.