3.Write Scripts for the mongo Shell-官方文档摘录
总结
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");
SEE ALSO
Differences Between Interactive and Scripted mongo
When writing scripts for the mongo shell, consider the following:
To set the
dbglobal variable, use thegetDB()method or theconnect()method. You can assign the database reference to a variable other thandb.Write operations in the
mongoshell use a write concern of { w: 1 } by default. If performing bulk operations, use theBulk()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
mongoshell helpers to their JavaScript equivalents.Shell Helpers JavaScript Equivalents show dbs,show databasesdb.adminCommand('listDatabases')use <db>db = db.getSiblingDB('<db>')show collectionsdb.getCollectionNames()
show usersdb.getUsers()
show rolesdb.getRoles({showBuiltinRoles: true})show log <logname>db.adminCommand({ 'getLog' : '<logname>' })show logsdb.adminCommand({ 'getLog' : '*' })itcursor = db.collection.find()
if ( cursor.hasNext() ){
cursor.next();
}In interactive mode,
mongoprints the results of operations including the content of all cursors. In scripts, either use the JavaScriptprint()function or themongospecificprintjson()function which returns formatted JSON.EXAMPLE
To print all items in a result cursor in
mongoshell 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-官方文档摘录的更多相关文章
- 2.Access the mongo Shell Help-官方文档摘录
总结: 1.使用help可以查看帮助信息db.help() help等 2.查看对应的实现方法.比如 test@gzxkvm52$ db.updateUser function (name, upd ...
- Cocos Creator 加载和切换场景(官方文档摘录)
Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...
- ng的概念层次(官方文档摘录)
官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...
- Cocos Creator 生命周期回调(官方文档摘录)
Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...
- Cocos Creator 使用计时器(官方文档摘录)
在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...
- angular 模板语法(官方文档摘录)
https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...
- 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 ...
- Spring Data Commons 官方文档学习
Spring Data Commons 官方文档学习 -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...
- Ionic2系列——Ionic 2 Guide 官方文档中文版
最近一直没更新博客,业余时间都在翻译Ionic2的文档.之前本来是想写一个入门,后来觉得干脆把官方文档翻译一下算了,因为官方文档就是最好的入门教程.后来越翻译越觉得这个事情确实比较费精力,不知道什么时 ...
随机推荐
- application/x-www-form-urlencoded接口响应报文中文乱码
1.如何处理乱码 在进行接口测试时,在用httpclient post请求时,对于Content-Type:application/json来说,在写测试脚本时只需要为头信息和post请求指定相应编码 ...
- ant.xml
<?xml version="1.0"?> <project name="dxcc" default="buildplugins&q ...
- 使用uGUI系统玩转标准俄罗斯方块
使用uGUI系统玩转标准俄罗斯方块 笔者使用的Unity3D版本是4.6b17.由于一些工作上的一些事情导致制作的进度被严重滞后.笔者实际用于开发俄罗斯方块的时间,大概也就2-3天吧. 开始前的准备 ...
- 制作Cubie版OpenWRT(功能齐全,大小仅有11M)
Allwinner Sun4i/5i/6i/7i (sunxi) Various vendors are offering development boards / single-board comp ...
- android最佳实践的建议(翻译自android-best-practices)
Best practices in Android development Use Gradle and its recommended project structure 使用Gradle和其推荐的 ...
- java 检测字符串中文乱码
1.检测是否为乱码 public static boolean isMessyCode(String strName) { Pattern p = Pattern.compile("\\s* ...
- flutter 环境
以下内容copy 于 https://www.cnblogs.com/lovelyYakir/p/7610396.html 原文请参考此 我只是做个记录 第一步:安装Git 你需要安装Git作为Flu ...
- Scala学习笔记(一)编程基础
强烈推荐参考该课程:http://www.runoob.com/scala/scala-tutorial.html 1. Scala概述 1.1. 什么是Scala Scala是一种多范式的编程 ...
- 线程同步工具CountDownLatch
CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行.假如我们这个想要继续往下执行的任务调用一个CountDownLatc ...
- Linux下vi命令小结
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi n filename :打开文件,并将光标置于第n行首 vi filename :打 ...