简体   繁体   English

mongoDB中的var用法

[英]var usage in mongoDB

I am learning mongoDB by following the tutorial, http://docs.mongodb.org/manual/tutorial/getting-started/ 我通过遵循http://docs.mongodb.org/manual/tutorial/getting-started/教程来学习mongoDB。

However, the sample code, var c = db.testData.find() , confuses me. 但是,示例代码var c = db.testData.find()使我感到困惑。 Without var, the execution just prints out the documents, with the var, c becomes the cursor. 如果没有var,则执行仅打印出文档,而使用var,c成为光标。 The other command, j = { name : "mongo" } , I don't see the difference with or without var. 另一个命令j = { name : "mongo" } ,无论是否带有var,我都看不到区别。 I want to be clear when I should have var and when I can ignore var. 我想弄清楚何时应该使用var,何时可以忽略var。

I google the question little bit, for example, Difference between using var and not using var in JavaScript , it talks about scope. 我在谷歌上搜索了一点问题,例如, 在JavaScript中使用var和不使用var之间的区别 ,它讨论了范围。 I cannot understand how my question relates to scope. 我不明白我的问题与范围的关系。

http://docs.mongodb.org/manual/tutorial/iterate-a-cursor/ http://docs.mongodb.org/manual/tutorial/iterate-a-cursor/

According to this doc, 根据这份文件,

However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results. 但是,在mongo shell中,如果未使用var关键字将返回的游标分配给变量,则该游标将自动迭代多达20次,以打印结果中的前20个文档。

From the mongo getting started tutorial: 从mongo入门教程:

This tutorial provides an introduction to basic database operations using the mongo shell. 本教程介绍了使用mongo shell进行的基本数据库操作。 mongo is a part of the standard MongoDB distribution and provides a full JavaScript environment with a complete access to the JavaScript language and all standard functions as well as a full database interface for MongoDB. mongo是标准MongoDB发行版的一部分,它提供了完整的JavaScript环境,可以完全访问JavaScript语言和所有标准功能,以及MongoDB的完整数据库接口。 See the mongo JavaScript API documentation and the mongo shell JavaScript Method Reference. 请参阅mongo JavaScript API文档和mongo shell JavaScript方法参考。

Source: http://docs.mongodb.org/manual/tutorial/getting-started/ 来源: http : //docs.mongodb.org/manual/tutorial/getting-started/

In Javascript it's best practice to not use global variables. 在Javascript中,最佳做法是不使用全局变量。 You define global variables without the var prefix and local variables with the var prefix. 您没有定义全局变量var前缀局部变量var前缀。 I guess the Javascript implementation in Mongo disabled global variables completely because it's bad practice. 我猜Mongo中的Javascript实现完全禁用了全局变量,因为这是一种不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM