MongoDB脚本的基础知识-如何 [英] Basics of MongoDB Scripts - How to
问题描述
MongoDB脚本的基本知识是什么?
What are the basics of MongoDB Scripts?
我认为脚本将以.js
结尾,我们使用mongo try.js
I think the script will end with .js
, and we run it using mongo try.js
但是如果我放
print(db.foo.find())
并使用 会说 ,如果我通过键入 and if I use the interactive shell by typing 做MongoDB脚本的正确方法是什么,以及如何像Ruby what are the proper ways of doing MongoDB Scripts and how to print things out like a Ruby 外部脚本文件是在shell上下文之外执行的. The external script files are executed outside of the shell context. The The 如果您需要打印查询结果,则必须在脚本文件中迭代游标并打印每个结果,类似于shell所做的事情: If you need to print results of the query, you'll have to iterate the cursor in your script file and print each result, similarly to what the shell does: 这篇关于MongoDB脚本的基础知识-如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!mongo try.js
MongoDB shell version: 1.6.1
connecting to: test
DBQuery: test.foo -> undefined
mongo
并键入mongo
and type> db.foo.find()
{ "_id" : ObjectId("4c7a73428261000000003a7e"), "a" : 1 }
> print(db.foo.find())
DBQuery: test.foo -> undefined
{a : 1}
是我之前使用db.foo.insert({a:1})
irb
或Python的IDLE
那样打印出来?谢谢. (Ruby的puts a.inspect
或p a
通常都可以打印出a
的整个结构(a
中的所有变量名称和值))irb
or Python's IDLE
? thanks. (Ruby's puts a.inspect
or p a
can both print out the whole structure of a
usually (all the variable names and values in a
))alert(db.foo.find())
和console.log(db.foo.find())
也不起作用.推荐答案
db.foo.find()
数据库命令仅返回游标;它本身不打印任何内容.当从外壳程序发出命令时,外壳程序将迭代光标并打印结果.从外部脚本文件运行命令时,不会打印任何内容.db.foo.find()
database command only returns a cursor; it doesn't print anything by itself. When the command is issued from the shell, the shell will iterate the cursor and print the results. When the command is run from an external script file, nothing is printed.print()
命令将打印出对象的字符串表示形式.在您的情况下,它是游标:print()
command will print out the string representation of the object. In your case, it's the cursor:> print(db.foo.find())
DBQuery: test.foo -> undefined
function printResult (r) {
print(tojson(r))
}
db.foo.find().forEach(printResult)