mongodb常用操作命令
2015-08-30 00:45
711 查看
在MongoDB 中,一个数据库可能会包含多个集合,就像MySql中一个数据库可能包含多个表;一个集合中可能又会包含多个文档,就像MySql中一个表中包含多条数据。
本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1689662
基本操作命令: show dbs 查看所有数据库列表 use <dbname> 连接指定的数据库 db 查看当前使用的数据库 > use testdb # 创建testdb数据库 switched to db testdb > show dbs admin 0.078GB ceilometer 1.953GB local 6.075GB 列出的数据库中没有testdb或者显示testdb(empty),是因为testdb里面没有任何东西。 > use testdb # 删除testdb数据库 switched to db testdb > db.dropDatabase() { "dropped" : "testdb", "ok" : 1 } > use testdb # 创建集合 switched to db testdb > db.createCollection('users') { "ok" : 1 } > show collections system.indexes users > show collections # 删除集合 system.indexes users > db.users.drop() true > show collections system.indexes > db.createCollection('users') # 往集合中插入数据,如果集合没有会自动创建 { "ok" : 1 } > show collections system.indexes users > db.users.insert([ ... { name : 'yao', ... email : 'yao@qq.com' ... }, ... { name: 'shen', ... email : 'shen@qq.com' ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.users.save([ # save也能实现上述insert的功能 ... { name : 'test', ... email : 'test@qq.com' ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 1, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > user1=({'name': 'yao2','email': 'yao2@qq.com'}) # 插入文档,当然也可以把文档内容直接作为函数参数来替代document { "name" : "yao2", "email" : "yao2@qq.com" } > db.users.insert(user1) WriteResult({ "nInserted" : 1 }) > db.users.update({"name":'yao2'},{$set:{'email':'yaotest@qq.com'}}) # 更新文档,需要引入关键字$set WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({'name':'yao2'}).pretty() { "_id" : ObjectId("55e262dc957ba26c00b19e42"), "name" : "yao2", "email" : "yaotest@qq.com" } > db.users.save({"_id":ObjectId("55e2673b957ba26c00b19e45"),"name":'yao2','email':'yaotest2@qq.com'}) # 替换存在的文档,但是update更好用点。 > db.users.remove({'name':'yao2'}) # 删除文档 WriteResult({ "nRemoved" : 4 }) > db.users.find({'name':'yao2'}).pretty()
数据查询命令: > db.users.find() # find不加任何参数返回所有数据记录 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "shen@qq.com" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "test@qq.com" } > db.users.find().pretty() # pretty查询输出结果更美观显示 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "shen@qq.com" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "test@qq.com" } > db.users.find({'name':'yao','email':'yao@qq.com'}).pretty() # find中传入多个键值对,mongodb针对多个键值对会当做and处理 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } > db.users.find({$or: [{'name':'yao'}, {'email':'test@qq.com'}]}).pretty() # or查询语句需要$or关键字 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "test@qq.com" } > db.users.find({'name': 'yao', $or: [{'name':'yao'}, {'email':'test@qq.com'}]}).pretty() # and、or一起用 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" }
带> >= < <=的条件查询,关键字$gt $lt $gte $lte(顾名思义) > db.users.find({'name':{$gt:'yao2'}}) > db.users.insert({'id':1,'name':'t1','email':'t1@qq.com'}) WriteResult({ "nInserted" : 1 }) > db.users.insert({'id':2,'name':'t2','email':'t2@qq.com'}) WriteResult({ "nInserted" : 1 }) > db.users.insert({'id':3,'name':'t3','email':'t3@qq.com'}) WriteResult({ "nInserted" : 1 }) > db.users.find({'id':{$gt:1}}) { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "t3@qq.com" } > db.users.find({'id':{$gte:1}}) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "t3@qq.com" } > db.users.find({'id':{$lte:2}}) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" } > db.users.find({'id':{$lt:2}}) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" } > db.users.find({'id':{$gt:1,$lt:3}}) { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" }
匹配type的条件查询 type值有: 双精度型(1) 字符串(2) 对象(3) 数组(4) 二进制数据(5) 对象ID(7) 布尔类型(8) 数据(9) 空(10) 正则表达式(11) JS代码(13) 符号(14) 有作用域的JS代码(15) 32位整型数(16) 时间戳(17) 64位整型数(18) Min key(255) Max key(127) > db.users.find({'name':{$type:2}}) { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "shen@qq.com" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "test@qq.com" } { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "t3@qq.com" }
limit与skip的使用,limit-读取指定数量的记录,skip-读取的时候跳过指定数量的记录 > db.users.find().limit(1) { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } > db.users.find().limit(1).skip(1) { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "shen@qq.com" } > db.users.find().limit(1).skip(3) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" } > db.users.find() { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "shen@qq.com" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "test@qq.com" } { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "t3@qq.com" }
sort排序,1表示升序,-1表示降序 > db.users.find().sort({'id':-1}) { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "t3@qq.com" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "t2@qq.com" } { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "t1@qq.com" }
本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1689662
相关文章推荐
- MongoDB(2)-基本概念
- 请不要再用MongoDB了
- 用node.js+express.js+mongodb+jade开发一个完整的项目笔记(1)【页面即页面之间逻辑的制作】
- MongoDB的安全(五)
- mongodb的基本操作
- MongoDB(4)--MongoDB服务的启动
- MongoDB学习十--MongoDB的Replication Introduction
- mongodb笔记
- windows下安装mongoDB扩展
- MongoDB语法与现有关系型数据库SQL语法比较
- mongoDB 基本命令整理
- 在centOS中安装mongodb
- mongodb安装及简单操作
- MongoDB数据库 基础知识
- 搭建高可用mongodb集群(二)—— 副本集
- MongoDB使用总结(C#版 潘鹏)
- 关于MongoDB数据库中文件唯一性的问题
- MongoDB 日期查询与mongodump 按日期范围导出数据
- Mongodb集群之分片存储
- MongoDB学习笔记