mongo数据库常用操作
1.连接数据库
mongo ip:port/databaseName -u user -p password
example:mongo ip:port/admin -u xxxxx -p xxxxx
2.数据库
2.1显示数据库
show dbs
2.2创建/使用数据库
use databaseName
2.3删除数据库
db.dropDatabase(); //删除当前使用的数据库
3.集合 Collection
3.1显示集合
show tables
3.2创建/使用集合
db.createCollection(name, options); //option可选
3.3删除集合
db.collectionName.drop();
4.记录 Document
4.1创建记录
db.collectionName.insert(document); //可同时插入多个记录,使用[document,document]
db.collectionName.save(document); //可同时插入多个记录,使用[document,document]
save和insert的区别
mongodb的save和insert函数都可以向collection里插入数据,但两者是有两个区别:
一、使用save函数里,如果原来的对象不存在,那他们都可以向collection里插入数据,如果已经存在,save会调用update更新里面的记录,其实就是直接覆盖,而insert则会忽略操作。
二、insert可以一次性插入一个列表,而不用遍历,效率高, save则需要遍历列表,一个个插入。
4.2删除记录
db.collectionName.remove({});
4.3更新记录
1.update()命令
db.collection.update( criteria, objNew, upsert, multi );
criteria:update的查询条件,类似sql update查询内where后面的。
objNew:update的对象和一些更新的操作符(如
,
,
,inc…)等,也可以理解为sql update查询内set后面的。
upsert:这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi:mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
2.更新操作符
-
KaTeX parse error: Expected '}', got 'EOF' at end of input: inc 用法:{inc:{field:value}}
含义:对field字段增加value。
例子:db.user.update({"_id":“1001”},{$inc:{“age”:1}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: set 用法:{set:{field:value}}
含义:相当于sql的set field = value,全部数据类型都支持KaTeX parse error: Expected '}', got 'EOF' at end of input: …"_id":"1001"},{set:{“name”:“lisi”,“company”:“xxxx.inc”}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: unset 用法:{unset:{field:1}}
含义:删除filed字段,后面填充的不一定是1,可以是其他任意内容。
例子:db.user.update({"_id":“1001”},{$unset:{“company”:1}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: push 用法:{push:{field:value}}
含义:把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去。
例子:db.user.update({"_id":“1001”},{$push,{“hobbies”:“reading”}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: pushAll 用法:{pushAll:{field:value_array}}
含义:同KaTeX parse error: Expected '}', got 'EOF' at end of input: …"_id":"1001"},{pushAll:{“hobbies”:[“drawing”,“driving bicycle”]}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: addToSet 用法:{addToSet:{field:value}}、{KaTeX parse error: Expected '}', got 'EOF' at end of input: …dToSet,{field:{each:value_array}}}
含义:增加一个值到数组内,而且只有当这个值不在数组内才增加。
例子:
db.user.update({"_id":“1001”},{KaTeX parse error: Expected 'EOF', got '}' at position 35: …:"watching TV"}}̲); db.user.up…addToSet:{$each:[“swimming”,“running”]}}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: …用法: 删除最后一个值:{pop:{field:1}}
删除第一个值:{KaTeX parse error: Expected 'EOF', got '}' at position 15: pop:{field:-1}}̲ 含义:只能删除一个值,也…pop:{“hobbies”:1}});
db.usser.update({"_id":“1001”},{$pop:{“hobbies”:-1}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: pull 用法:{pull,{field:value}}
含义:从数组field内删除一个value的值。
例子:db.user.update({"_id":“1001”},{$pull,{“hobbies”:“reading”}}); -
KaTeX parse error: Expected '}', got 'EOF' at end of input: pullAll 用法:{pullAll:{field:value_array}}
含义:从数组field内一次删除多个值。
例子:db.user.update({"_id":“1001”},{$pullAll:{“hobbbies”:[“watching TV”,“drawing”]}}); -
$
含义: 是 他 自 己 的 意 思 , 代 表 按 条 件 找 出 来 的 数 组 里 面 某 项 他 自 己 。 需 要 注 意 的 是 , ¥ 只 会 应 用 找 到 的 第 一 条 数 据 项 , 后 面 的 就 不 管 了 , 是他自己的意思,代表按条件找出来的数组里面某项他自己。需要注意的是,¥只会应用找到的第一条数据项,后面的就不管了, 是他自己的意思,代表按条件找出来的数组里面某项他自己。需要注意的是,¥只会应用找到的第一条数据项,后面的就不管了,配合$unset使用的时候,会留下一个null的项,可以使用pushAll删除所有是null的项。
例子:
t.find()
{ “_id” : ObjectId(“4b97e62bf1d8c7152c9ccb74”), “title” : “ABC”, “comments” : [ { “by” : “joe”, “votes” : 3 }, { “by” : “jane”, “votes” : 7 } ] }
t.update( {‘comments.by’:‘joe’}, {KaTeX parse error: Expected '}', got 'EOF' at end of input: inc:{'comments..votes’:1}}, false, true )
t.find()
{ “_id” : ObjectId(“4b97e62bf1d8c7152c9ccb74”), “title” : “ABC”, “comments” : [ { “by” : “joe”, “votes” : 4 }, { “by” : “jane”, “votes” : 7 } ] }
t.insert({x: [1,2,3,4,3,2,3,4]})
t.find()
{ “_id” : ObjectId(“4bde2ad3755d00000000710e”), “x” : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
t.update({x:3}, {KaTeX parse error: Expected '}', got 'EOF' at end of input: unset:{"x.":1}})
t.find()
{ “_id” : ObjectId(“4bde2ad3755d00000000710e”), “x” : [ 1, 2, null, 4, 3, 2, 3, 4 ] }
{ “_id” : ObjectId(“4b9e4a1fc583fa1c76198319”), “x” : [ 1, 3, 3, 2 ] }
4.4查询记录
http://www.cnblogs.com/egger/archive/2013/06/14/3135847.html#indoc
查询操作符:
“
g
t
"
、
"
gt" 、"
gt"、"gte”、 “
l
t
"
、
"
lt"、 "
lt"、"lte”、“null查询”、“
a
l
l
"
、
"
all"、"
all"、"size”、“
i
n
"
、
"
in"、"
in"、"nin”、
“
a
n
d
"
、
"
and"、"
and"、"nor”、“
n
o
t
"
、
"
not"、"
not"、"or”、“
e
x
i
s
t
s
"
、
"
exists"、"
exists"、"mod”、“
r
e
g
e
x
"
、
"
regex"、"
regex"、"where”、“
s
l
i
c
e
"
、
"
slice"、"
slice"、"elemMatch”
模糊查询:db.user.find({“name”:{KaTeX parse error: Expected group after '^' at position 8: regex:/^̲\w{1}/,$options:‘i’}});
4.5 其它
- 计数
用法:db.collectionName.find({}).count(); - 排序
用法:db.collectionName.sort({field:1,field:-1});
含义:1代表升序,-1代表降序。 - 分页
用法:db.collectionName.sort().sike(number1).limit(number2);
含义:排序后,跳过number1行后,查询出number2行,如db.collectionName.sort().sike(10).limit(10);则查询出第10-19条记录。 - 批量导入数据
用法:for(var i = 0 ;i < 2000000;i++){db.user.save({“index”:i,“name”:“zhangsan”+i,“age”:(i+10)%100});} - shell中查看
因为查询记录条数大于20时,命令窗口只显示钱20条,使用it命令可以查看下一页。 - 执行计划
用法:db.collectionName.find({}).explain()
5.索引
5.1创建索引
5.2查询索引
5.3删除索引
5.4使用索引
6.存储引擎
http://www.cnblogs.com/ljhdo/archive/2016/10/30/4947357.html
6.1WiredTiger
mongod
–storageEngine wiredTiger
–dbpath
–journal --wiredTigerCacheSizeGB
–wiredTigerJournalCompressor
–wiredTigerCollectionBlockCompressor
–wiredTigerIndexPrefixCompression
6.2In-Memery
mongod
–storageEngine inMemory
–dbpath
–inMemorySizeGB
–replSet
–oplogSize
7.数据导入导出
7.1数据导出
mongoexport --help 查看
示例:mongoexport --db war_8001 --collection game_users --query {"_id":“8001_501”} --out ~/Desktop/out2.json
7.2导入数据
mongoimport --help
示例:mongoimport --db war_8001 --collection game_users --type json ~/Desktop/8001_501.json --upsert
8.Console执行
http://www.jincon.com/archives/95/
查询结果翻页:it
设置查询显示的最大条数:DBQuery.shellBatchSize= 50;