mongo shell

mongo 连接

本地

mongo # 连接127.0.0.1:27017

远程

mongo "mongodb://mongodb0.example.com:28015"

带认证的方式

mongo "mongodb://alice@mongodb0.examples.com:28015/?authSource=admin"

复制集

mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
mongo "mongodb+srv://server.example.com/" # DNS Seedlist Connection Format

SSL方式

mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA&ssl=true"

mongo shell 操作

db # 查看当前数据库
use <database> #使用数据库

当插入数据时会自动创建集合

在mongo第一次显示提示之前,会自动运行HOME目录下的.mongorc.js文件,如果不想加载,可以使用--norc参数

配置shell

cmdCount = 1;
prompt = function() {
return (cmdCount++) + "> ";
}
# 显示行数

Date

  • Date() 返回string
  • new Date() 返回ISODate
  • ISODate() 返回ISODate

ObjectId

_id的类型

NumberLong

mongo shell中,默认认为数字为浮点型,为表示64位int型数据,可以用NumberLong

如果用$inc作用于NumberLone,将把此数转为浮点型

数字比较

Query Results
{ “val”: 9.99 } { “_id”: 2, “val”: 9.99, “description”: “Double” }
{ “val”: NumberDecimal( “9.99” ) } { “_id”: 1, “val”: NumberDecimal( “9.99” ), “description”: “Decimal” }
{ val: 10 } { “_id”: 3, “val”: 10, “description”: “Double” }{ “_id”: 4, “val”: NumberLong(10), “description”: “Long” }{ “_id”: 5, “val”: NumberDecimal( “10.0” ), “description”: “Decimal” }
{ val: NumberDecimal( “10” ) } { “_id”: 3,“val”:10, “description”: “Double” }{ “_id”: 4, “val”: NumberLong(10), “description”: “Long” }{ “_id”: 5, “val”: NumberDecimal( “10.0” ), “description”: “Decimal” }

当double类型的数据进行比较时,会转换double的数据类型为decimal,而且会有误差

可以用type进行比对

db.inventory.find( { price: { $type: "decimal" } } )

shell操作指南

~/.dbshell:操作 记录

show dbs:查看 所有 数据库,不同的访问权限会有不同的结果

use <db>:使用数据库

show collections:查看所有集合

show users:查看当前所有用户

show roles:查看当前数据库所有角色,包括内建的

show profile:打印耗时1毫秒以上的操作5个

show databases:查看可访问的所有数据库

mongo shell的更多相关文章

  1. MongoDB error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js(转)

    rror: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js 一般这种情况就是:自己指定的数据库,所以不能.自动加 ...

  2. 常见的mongo shell命令

    启动mongo shell 在windows下,双击mongo.exe可以启动mongo shell 查询库.表及选择库 查询所有库命令: show dbs 应用某一个db use jxs_datab ...

  3. MongoDB - The mongo Shell, mongo Shell Quick Reference

    mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up ...

  4. MongoDB - The mongo Shell, Data Types in the mongo Shell

    MongoDB BSON provides support for additional data types than JSON. Drivers provide native support fo ...

  5. 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 ...

  6. MongoDB - The mongo Shell, Access the mongo Shell Help

    In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional inf ...

  7. MongoDB - The mongo Shell, Configure the mongo Shell

    Customize the Prompt You may modify the content of the prompt by setting the variable prompt in the  ...

  8. MongoDB - Introduction of the mongo Shell

    Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mong ...

  9. couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145

    当直接执行./mongo 出现这样的提示:couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 解决: ...

  10. mongo Shell初体验

    mongo shell是一个MongoDB的交互式JavaScript接口.您可以使用mongo shell来查询和更新数据以及执行管理操作. 打开cmd命令行,输入mongo,就可以进入mongo ...

随机推荐

  1. AcWing 785.快速排序

    AcWing 785.快速排序题解 题目描述 给定你一个长度为n的整数数列. 请你使用快速排序对这个数列按照从小到大进行排序. 并将排好序的数列按顺序输出. 输入格式 输入共两行,第一行包含整数 n. ...

  2. 通过指针突破C++类的访问权限

    看如下代码 #include "pch.h" #include <iostream> using namespace std; class A { public: A( ...

  3. DotNetty发送请求的最佳实践

    长链接发送request/response时, 绝大部分包都是小包, 而每个小包都要消耗一个IP包, 成本大约是20-30us, 普通千兆网卡的pps大约是60Wpps, 所以想要提高长链接密集IO的 ...

  4. linux安装mariadb

    yum install -y mariadb-server 账号:root 密码:空 本地登录:mysql -u root -p 远程登录:mysql -h 192.168.0.1 -u root - ...

  5. VC实现快递查询

    #include <iostream> #include <string> #include <cstdlib> #include <afxinet.h> ...

  6. 通过Python包来剪枝、蒸馏DNN

    用 Distiller 压缩 PyTorch 模型 作者: PyTorch 中文网发布: 2018年7月15日 5,101阅读 0评论 近日,Intel 开源了一个用于神经网络压缩的开源 Python ...

  7. learn about sqlserver files and filegroup

    The filegroup is similar as tablespace in Oracle. At first, I will show that hot to check file amd f ...

  8. java架构之路-(微服务专题)初步认识微服务与nacos初步搭建

    历史演变: 以前我们都是一个war包,包含了很多很多的代码,反正我开始工作的时候做的就是这样的项目,一个金融系统,代码具体多少行记不清楚了,内部功能超多,但是实际能用到的不多,代码冗余超大,每次部署大 ...

  9. MySQL中遍历查询结果的常用API(c)

    本中所使用的table: MySQL中的错误处理函数 unsigned int mysql_errno(MYSQL *mysql) const char *mysql_error(MYSQL *mys ...

  10. 使用Java, AppleScript对晓黑板进行自动打卡

    使用Java, AppleScript对晓黑板进行自动打卡 由于我们学校要求每天7点起床打卡,但是实在做不到,遂写了这个脚本. 绪论 由于晓黑板不支持网页版,只能使用App进行打卡,所以我使用网易的安 ...