[英]MongoDB - eval Vs. db.eval
I have a simple stored script in system.js collection called getAllNotes. 我在system.js集合中有一个简单的存储脚本,称为getAllNotes。
script code: function (x) { return db.notes.find(); }
脚本代码: function (x) { return db.notes.find(); }
function (x) { return db.notes.find(); }
notes is a collection holding the data I wish to extract. notes是包含我要提取的数据的集合。
eval('getAllNotes()')
- works well and returns the data. eval('getAllNotes()')
-运作良好并返回数据。
db.eval('getAllNotes()')
- returns a JSON string with various configuration that has nothing to do with my collection See below. db.eval('getAllNotes()')
-返回具有各种配置的JSON字符串,这些配置与我的收藏无关。请参见下文。 Any idea? 任何想法? anyone understands the difference between executing eval and db.eval? 任何人都了解执行eval和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. eval
接受执行的代码字符串。
db.eval
, according to the docs , takes a function to call and a list of parameters to pass to that function. 根据docs , db.eval
需要一个函数来调用,并将参数列表传递给该函数。 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. mongo shell中的帮助程序db.eval()
包装了eval
命令。 Therefore, the helper method shares the characteristics and behavior of the underlying command with one exception:db.eval()
method does not support thenolock
option. 因此,辅助方法共享基本命令的特性和行为,但有一个例外:db.eval()
方法不支持nolock
选项。
Docs. 文件。 Important stuff to learn things from before asking other people to do it for you. 在要求别人为您做事之前要学习的重要知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.