总结

1 使用js进行获取数据的方法

2 js方式和原生mongo shell的交互方式的区别写法

3 需要将所有数据打印出来使用到的循环示例

cursor = db.collection.find();
while ( cursor.hasNext() ) {
printjson( cursor.next() );
}

4 介绍怎么外部执行js文件

5 介绍怎么在mongo shell中加载js文件

You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform administrative operation. For more information about the mongo shell, see the Running .js files via a mongo shell Instance on the Server section for more information about using these mongo script.

This tutorial provides an introduction to writing JavaScript that uses the mongo shell to access MongoDB.

Opening New Connections

From the mongo shell or from a JavaScript file, you can instantiate database connections using the Mongo()constructor:

new Mongo()
new Mongo(<host>)
new Mongo(<host:port>)

Consider the following example that instantiates a new connection to the MongoDB instance running on localhost on the default port and sets the global db variable to myDatabase using the getDB() method:

conn = new Mongo();
db = conn.getDB("myDatabase");

If connecting to a MongoDB instance that enforces access control, you can use the db.auth() method to authenticate.

Additionally, you can use the connect() method to connect to the MongoDB instance. The following example connects to the MongoDB instance that is running on localhost with the non-default port 27020and set the global db variable:

db = connect("localhost:27020/myDatabase");

Differences Between Interactive and Scripted mongo

When writing scripts for the mongo shell, consider the following:

  • To set the db global variable, use the getDB() method or the connect() method. You can assign the database reference to a variable other than db.

  • Write operations in the mongo shell use a write concern of { w: 1 } by default. If performing bulk operations, use the Bulk() methods. See Write Method Acknowledgements for more information.

    Changed in version 2.6: Before MongoDB 2.6, call db.getLastError() explicitly to wait for the result of write operations.

  • You cannot use any shell helper (e.g. use <dbname>show dbs, etc.) inside the JavaScript file because they are not valid JavaScript.

    The following table maps the most common mongo shell helpers to their JavaScript equivalents.

    Shell Helpers JavaScript Equivalents
    show dbsshow databases
    db.adminCommand('listDatabases')
    
    use <db>
    db = db.getSiblingDB('<db>')
    
    show collections
    db.getCollectionNames()
    
    show users
    db.getUsers()
    
    show roles
    db.getRoles({showBuiltinRoles: true})
    
    show log <logname>
    db.adminCommand({ 'getLog' : '<logname>' })
    
    show logs
    db.adminCommand({ 'getLog' : '*' })
    
    it
    cursor = db.collection.find()
    if ( cursor.hasNext() ){
    cursor.next();
    }
  • In interactive mode, mongo prints the results of operations including the content of all cursors. In scripts, either use the JavaScript print() function or the mongo specific printjson() function which returns formatted JSON.

    EXAMPLE

    To print all items in a result cursor in mongo shell scripts, use the following idiom:

    cursor = db.collection.find();
    while ( cursor.hasNext() ) {
    printjson( cursor.next() );
    }

Scripting

From the system prompt, use mongo to evaluate JavaScript.

--eval option

Use the --eval option to mongo to pass the shell a JavaScript fragment, as in the following:

mongo test --eval "printjson(db.getCollectionNames())"

This returns the output of db.getCollectionNames() using the mongo shell connected to the mongod ormongos instance running on port 27017 on the localhost interface.

Execute a JavaScript file

You can specify a .js file to the mongo shell, and mongo will execute the JavaScript directly. Consider the following example:

mongo localhost:27017/test myjsfile.js

This operation executes the myjsfile.js script in a mongo shell that connects to the test database on the mongod instance accessible via the localhost interface on port 27017.

Alternately, you can specify the mongodb connection parameters inside of the javascript file using the Mongo() constructor. See Opening New Connections for more information.

You can execute a .js file from within the mongo shell, using the load() function, as in the following:

load("myjstest.js")

This function loads and executes the myjstest.js file.

The load() method accepts relative and absolute paths. If the current working directory of the mongo shell is /data/db, and the myjstest.js resides in the /data/db/scripts directory, then the following calls within the mongo shell would be equivalent:

load("scripts/myjstest.js")
load("/data/db/scripts/myjstest.js")

NOTE

There is no search path for the load() function. If the desired script is not in the current working directory or the full specified path, mongo will not be able to access the file.

3.Write Scripts for the mongo Shell-官方文档摘录的更多相关文章

  1. 2.Access the mongo Shell Help-官方文档摘录

    总结: 1.使用help可以查看帮助信息db.help()  help等 2.查看对应的实现方法.比如 test@gzxkvm52$ db.updateUser function (name, upd ...

  2. Cocos Creator 加载和切换场景(官方文档摘录)

    Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...

  3. ng的概念层次(官方文档摘录)

    官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...

  4. Cocos Creator 生命周期回调(官方文档摘录)

    Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...

  5. Cocos Creator 使用计时器(官方文档摘录)

    在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...

  6. angular 模板语法(官方文档摘录)

    https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...

  7. MongoDB - The mongo Shell, Write Scripts for the mongo Shell

    You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform a ...

  8. Spring Data Commons 官方文档学习

    Spring Data Commons 官方文档学习   -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...

  9. Ionic2系列——Ionic 2 Guide 官方文档中文版

    最近一直没更新博客,业余时间都在翻译Ionic2的文档.之前本来是想写一个入门,后来觉得干脆把官方文档翻译一下算了,因为官方文档就是最好的入门教程.后来越翻译越觉得这个事情确实比较费精力,不知道什么时 ...

随机推荐

  1. MVC & Entity Framework(2)- controller、Models单独DLL

    继上一篇MVC & Entity Framework(1)- 开发环境之后,已经很久没更新了.接下来记录一下怎么把MVC中的controller单独拆为一个类库,然后在web项目中引用.另外, ...

  2. jsp tld的function 自定义方法扩展

    引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写法示例: <? ...

  3. apache commons io入门

    原文参考  http://www.javacodegeeks.com/2014/10/apache-commons-io-tutorial.html    Apache Commons IO 包绝对是 ...

  4. 猫猫学iOS 之微博项目实战(5)微博自己定义搜索框searchBar

    猫猫分享.必须精品 原创文章.欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 用UITextField简单定义一个搜索框 二:调用 ...

  5. Solr 多字段、打分规则、权重和实时索引同步

    1.字段 Filed:<field name="_id" type="text_ik" indexed="true" stored=& ...

  6. 【shell】tar命令详解

    tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件! ...

  7. 解决linux下/etc/rc.local开机器不执行的原因

    前不久因项目需要写了开机启动其他程序的shell脚本,因工作忙,调试完给了技术支持人员,也没去注意过. 到后来,有几台服务器突然被重启了,这时候领导问,怎么开机启动的脚本没起作用,还被批了一顿,哎,做 ...

  8. jQuery插件学习笔记

    近期在研究jQuery插件,插件编写的目的是给已经有的一系列方法或函数做一个封装,以便在其它地方反复使用,方便后期维护. JQuery除了提供一个简单.有效的方式进行管理元素以及脚本,它还还提供了例外 ...

  9. Iptables规则执行顺序详解

      1.The first is the mangle table which is responsible for the alteration of quality of service bits ...

  10. mybatis之高级结果映射

    先贴一句官方文档内容 如果世界总是这么简单就好了. 正如官方文档所说:如果一切都是这么简单,那该多好啊,但是实际上,我们面对的是复杂的对象,就是对象里有对象,有列表对象,总之五花八门的对象.这个时候我 ...