前言

前段时间. 实战操作了下MongoDB. 在此将操作记录总结一下.


MongoDB 相关Bin

localhost:bin sean$ pwd
/Users/sean/Software/MongoDB/current/bin
localhost:bin sean$ ls
Untitled.js mongo       mongodump   mongofiles  mongooplog  mongoreplay mongos      mongotop
bsondump    mongod      mongoexport mongoimport mongoperf   mongorestore    mongostat

进入MongoDB的bin目录下, 我们发现MongoDB提供了如下工具.
PS: Windows平台部分命令没有, 需要安装单独的工具包。

  • mongod: mongoDB Server端.
  • mongo: client端
  • mongoimport 导入
  • mongoexport 导出

本篇主要就是基于mongo命令基础上, 后续进行操作.


连接

先通过--help看下基础命令.

localhost:bin sean$ ./mongo --help
MongoDB shell version v3.4.2
usage: ./mongo [options] [db address] [file names (ending in .js)]
db address can be:foo                   foo database on local machine192.168.0.5/foo       foo database on 192.168.0.5 machine192.168.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
Options:--shell                             run the shell after executing files--nodb                              don't connect to mongod on startup - no'db address' arg expected--norc                              will not run the ".mongorc.js" file onstart up--quiet                             be less chatty--port arg                          port to connect to--host arg                          server to connect to--eval arg                          evaluate javascript-h [ --help ]                       show this usage information--version                           show version information--verbose                           increase verbosity--ipv6                              enable IPv6 support (disabled by default)--disableJavaScriptJIT              disable the Javascript Just In Timecompiler--disableJavaScriptProtection       allow automatic JavaScript functionmarshalling--networkMessageCompressors arg     Comma-separated list of compressors touse for network messages--jsHeapLimitMB arg                 set the js scope's heap size limitAuthentication Options:-u [ --username ] arg               username for authentication-p [ --password ] arg               password for authentication--authenticationDatabase arg        user source (defaults to dbname)--authenticationMechanism arg       authentication mechanism--gssapiServiceName arg (=mongodb)  Service name to use when authenticatingusing GSSAPI/Kerberos--gssapiHostName arg                Remote host name to use for purpose ofGSSAPI/Kerberos authenticationfile names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
  • 无密码参数
    ./mongo 127.0.0.1:27017

  • 有密码授权
    ./mongo -u username -p password --authenticationDatabase authDataBaseName 127.0.0.1:27017


基本结构查询

  • 查询数据库 - show dbs

  • 查询数据库表 - show collections


简单查询

  • find() - db.col_name.find()

查询操作

  • pretty() - db.col_name.find().pretty()

将查询结果拆散. 更容易观看.

  • count() - db.col_name.find().count()

个数统计.

  • limit() - db.col_name.find().limit(num)

查询结果个数限制.

  • 索引相关

表默认会根据'_id'字段建立索引. 如果需要建立其他索引, 可通过如下命令.

db.col_name.createIndex({"title":1})
  • 内嵌函数体
  • $in 操作符使用
$in
The $in operator selects the documents where the valueof a field equals any >value in the specified array. To specify an$in expression, use the following >prototype:For comparison of different BSON type values, see the specifiedBSON >comparison order.{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
If the field holds an array, then the $in operatorselects the documents whose field holds an array that containsat least one element that matches a value in the specified array(e.g. <value1>, <value2>, etc.)上面这段话是摘自官方的解释,大概的意思就是说:field只要和array中的任意一个value相同,那么该文档就会被检索出来。也就是说mongoDB中$in的功能和sql中的in是相同的。

MongoDB之$in的用法

db.user.find({hobby:{$in:["足球","篮球"]  } })
  • $or & $all
> db.user.find({$or:[{name:"mike"},{name:"jack"}]} )
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }> db.user.find({hobby:{$all:["足球"]  } })
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }> db.user.find({hobby:{$all:["足球","羽毛球"]  } })
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }> db.user.find({hobby:{$all:["足球","桌球"]  } })

MongoDB 查询$关键字 $in $or $all


Others

ObjectId 与 String类型的Id

略. 值得注意的是MongoDB内的ID为ObjectID类型. 且长度固定24位. 不足24位的ID, MongoDB会将其指定为一个String类型. 这在查询的时候有所影响, 需要特别注意.

mongodb中的objectId和字符串id之间的转换

mongodb – 将ObjectID转换为mongo聚合中的字符串

MongoDB深究之ObjectId


Schema查询

MongoDB内是不存在Schema的概念的. 但是有的时候, 我们需要对其中对表结构进行统计. 这里, MongoDB提供了Varity.js进行查询. 具体操作如下:

  • 安装过程

variety.js安装到想要安装的相关目录.
https://github.com/variety/variety

  • 操作过程
./mongo 192.168.10.1/TESTS --eval "var collection = 'TESTS ', query = {'mediaTypeId':'Atlas'} /opt/variety-master/variety.js

[2]. Variety.js


Reference

[1]. 菜鸟教程 - MongoDB 教程

[MongoDB] 运维相关操作记录相关推荐

  1. linux运维相关操作(centos/Ubuntu)

    1.centos 网络配置相关的文件 /etc/hostname 主机名配置 /etc/sysconfig/network-scrips/ifcfg-enoN 网卡参数配置 /etc/resolv.c ...

  2. Open edX 学习、开发、运维相关链接整理

    原文地址:http://edustack.org/ 所需知识: Linux Git Python (Django Mako coffeescript sass) (MongoDB Mysql) Ans ...

  3. Linux基本命令-权限、运维相关

    Linux基本命令-权限.运维相关 一.权限 二.运维 查看进程 查看网络信息 查看系统运行资源 查看系统磁盘和分区信息 查看系统信息 查看用户信息 telenet命令 ping命令 curl命令 一 ...

  4. 阿里云官方学习课程推荐-Linux运维学习路线 从事云计算运维相关工作必备技能

    阿里云官方学习课程推荐-Linux运维学习路线 从事云计算运维相关工作必备技能 目前越来越多的企业需要依赖于IT技术发布产品与服务,尤其是电子商务最为明显,它凸显了IT技术在现代企业中的重要性.当企业 ...

  5. 基于Python2.7的阿里云API调用及运维相关

    基于Python2.7的阿里云API调用及运维相关 http://blog.chinaunix.net/uid-17291169-id-5212576.html 公司部分业务放在阿里云上面,虽然阿里云 ...

  6. IT运维相关技术文档

    IT运维相关技术文档 PowerShell文件系统(二)访问文件和目录 CentOS7离线安装SNMP服务 Wind River Linux Installed and Available Softw ...

  7. linux运维工程师培训课程_【课程培训】坐标6月份网络技术培训班开课了!数据库服务器运维相关经验...

    信息时代的飞速发展使得"信息安全"上升到了一个前所未有的高度.计算机里的数据就像扫雷游戏一样,十面埋伏充满雷区,随时都有可能Game Over,因此,信息安全的核心在于数据安全,数 ...

  8. 运维相关环境与工具扫盲

    项目的发布 开发环境 devel(DEV) Development Environment 又称软件开发环境 (Software Development Environment,SDE) 是程序猿们专 ...

  9. 又一个程序员被判刑了!运维违规操作被判5年半,IT从业需要懂法律!

    程序员的成长之路 互联网/程序员/技术/资料共享 关注 阅读本文大概需要 15 分钟. 来自:云头条 夏某某任职北京中科某某科技有限公司,负责该公司为郑大一附院开发的"软件信息系统" ...

最新文章

  1. 感謝有PPStream這種好東西
  2. Android---自定义Toast
  3. Linux用户与“最小权限”原则
  4. HDU 4753 Fishhead’s Little Game(DFS)
  5. “拯救网站运维经理赵明”已近尾声
  6. Asp.Net中的正则表达式问题可以在此提问,今后大家一起研究!贴出基本语法参考...
  7. win10使用nssm注册服务
  8. 卡尔曼滤波算法 C语言实现
  9. 基于K-means的彩色图像聚类之代码实现
  10. oCPC实践录 | 成本优化策略之eCPC(1)
  11. 汇编语言,两个数字的想加_8085微处理器中的汇编语言程序将两个16位数字相乘...
  12. GNN手写字体识别java_深度之眼Paper带读笔记GNN.09.GGNN
  13. JAVA_小小圣诞树
  14. 面试官偷偷给我的软件测试工程师面试题,看完你还怕拿不到offer?
  15. 路漫漫其修远兮,吾将上下而求索---我的五年工作总结
  16. flex与grid响应式布局中的子容器为固定空间后的剩余空间,该元素容器不出现滚动条或者滚动条异常问题
  17. ps图片怎样实现渐变
  18. springboot集成groovy脚本环境
  19. “人工智能与人类社会”栏目约稿函暨论文汇总2022-2017
  20. ps3本服务器维修中,PS3主机部分错误代码一览

热门文章

  1. 《迅雷链精品课》第十五课:共识算法的性能问题
  2. python安卓-如何用python写一个安卓APP?(上)
  3. 程序人生 - 2020年杭州市积分入学实施办法权威解读
  4. 火狐浏览器屏蔽百度热榜的方法
  5. Ralink SDK相关指令总结
  6. java算术运算符乘方_用java编写计算器 能够实现简单的加、减、乘、除、乘方、开方运算等;...
  7. NGFW中数据包转发流程
  8. mysql数据库存储引擎
  9. 中国男人到底配不配得上中国…
  10. visio常用快捷键_Visio实用快捷键+比较不错的总结