在MongoDB 中,一个数据库可能会包含多个集合,就像MySql中一个数据库可能包含多个表;一个集合中可能又会包含多个文档,就像MySql中一个表中包含多条数据。
基本操作命令: 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 : '[email protected]' ... }, ... { name: 'shen', ... email : '[email protected]' ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.users.save([ # save也能实现上述insert的功能 ... { name : 'test', ... email : '[email protected]' ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 1, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > user1=({'name': 'yao2','email': '[email protected]'}) # 插入文档,当然也可以把文档内容直接作为函数参数来替代document { "name" : "yao2", "email" : "[email protected]" } > db.users.insert(user1) WriteResult({ "nInserted" : 1 }) > db.users.update({"name":'yao2'},{$set:{'email':'[email protected]'}}) # 更新文档,需要引入关键字$set WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({'name':'yao2'}).pretty() { "_id" : ObjectId("55e262dc957ba26c00b19e42"), "name" : "yao2", "email" : "[email protected]" } > db.users.save({"_id":ObjectId("55e2673b957ba26c00b19e45"),"name":'yao2','email':'[email protected]'}) # 替换存在的文档,但是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" : "[email protected]" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "[email protected]" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "[email protected]" } > db.users.find().pretty() # pretty查询输出结果更美观显示 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "[email protected]" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "[email protected]" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "[email protected]" } > db.users.find({'name':'yao','email':'[email protected]'}).pretty() # find中传入多个键值对,mongodb针对多个键值对会当做and处理 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "[email protected]" } > db.users.find({$or: [{'name':'yao'}, {'email':'[email protected]'}]}).pretty() # or查询语句需要$or关键字 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "[email protected]" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "[email protected]" } > db.users.find({'name': 'yao', $or: [{'name':'yao'}, {'email':'[email protected]'}]}).pretty() # and、or一起用 { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "[email protected]" }
带> >= < <=的条件查询,关键字$gt $lt $gte $lte(顾名思义) > db.users.find({'name':{$gt:'yao2'}}) > db.users.insert({'id':1,'name':'t1','email':'[email protected]'}) WriteResult({ "nInserted" : 1 }) > db.users.insert({'id':2,'name':'t2','email':'[email protected]'}) WriteResult({ "nInserted" : 1 }) > db.users.insert({'id':3,'name':'t3','email':'[email protected]'}) WriteResult({ "nInserted" : 1 }) > db.users.find({'id':{$gt:1}}) { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "[email protected]" } > db.users.find({'id':{$gte:1}}) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "[email protected]" } > db.users.find({'id':{$lte:2}}) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" } > db.users.find({'id':{$lt:2}}) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" } > db.users.find({'id':{$gt:1,$lt:3}}) { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" }
匹配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" : "[email protected]" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "[email protected]" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "[email protected]" } { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "[email protected]" }
limit与skip的使用,limit-读取指定数量的记录,skip-读取的时候跳过指定数量的记录 > db.users.find().limit(1) { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "[email protected]" } > db.users.find().limit(1).skip(1) { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "[email protected]" } > db.users.find().limit(1).skip(3) { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" } > db.users.find() { "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "[email protected]" } { "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "[email protected]" } { "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "[email protected]" } { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" } { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "[email protected]" }
sort排序,1表示升序,-1表示降序 > db.users.find().sort({'id':-1}) { "_id" : ObjectId("55e26c2f957ba26c00b19e48"), "id" : 3, "name" : "t3", "email" : "[email protected]" } { "_id" : ObjectId("55e26c24957ba26c00b19e47"), "id" : 2, "name" : "t2", "email" : "[email protected]" } { "_id" : ObjectId("55e26c0a957ba26c00b19e46"), "id" : 1, "name" : "t1", "email" : "[email protected]" }