总结

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. 网络结构----ISO/OSI七层网络模型和TCP/IP四层网络模型

    对等实体在一次交互作用中传送的信息单位称为协议数据单元.它包含控制信息和用户数据两部分. 上下层实体之间的接口称为服务訪问点. ISO/OSI參考模型分为:物理层.数据链路层.网络层.传输层.会话层. ...

  2. ExtPager ,分页

    package cn.edu.hbcf.common.vo; public class ExtPager { private Integer start; private Integer limit; ...

  3. 免安装Oracleclient和PL/SQL

    写在前面: Oracle是典型的C/S结构,服务端提供oracle服务的实例,主要用于数据库的管理,对象的管理与存储.数据的 存储.查询.数据库资源的监控.监听等一些服务. 而client仅仅是一个与 ...

  4. Mongodb 和 Solr 实时同步

    一.安装前准备 1.mongo-connector(基于python)中间件 2.python-3.4.3.msi 3.Mongodb 4.Solr 二.配置Mongodb集群 1).配置replic ...

  5. Vim 学习笔记二

    1. 粘帖 p 光标前 P 2. 撤销对撤销的撤销 Ctrl+r 3. dl:删除一个字符,daw:删除一个单词,dap:删除一个段落 4. 单个c字符并无效果,cc删除整个一行 C:从当前光标出删除 ...

  6. 木马suppoie 处理的几个思路 木马文件的权限所有者 属主数组 定时任务 目录权限

    木马suppoie 处理的几个思路  木马文件的权限所有者  属主数组  定时任务   目录权限

  7. linux中常用压缩与解压命令

    一. tar文件的解压 tar -xvf db.tar 二. zip文件的压缩与解压 压缩 zip -r dest_name.zip directory_to_compress 解压 unzip de ...

  8. ashx一般处理程序

    说明:    虽然通过标准的方式可以创建处理程序,但是实现的步骤比较复杂,为了方便网站开发中对处理程序的应用,从Asp.net 2.0开始,asp.net提供了称为一般处理程序的处理程序,允许我们使用 ...

  9. C#从Excel中读取数据为空

    将HDR设置为YES,IMEX设置为1即可. OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.ACE.OL ...

  10. 设置EntityFramework中decimal类型数据精度

    EF中默认的decimal数据精度为两位数,当我们数据库设置的精度大于2时,EF将只会保留到2为精度. e.g. 2.1999将会被保存为2.20 网上找到常见的方法为重写DbContext的OnMo ...