重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
MongoDB常用命令汇总(一)
创新互联公司是一家集网站建设,兴隆台企业网站建设,兴隆台品牌网站建设,网站定制,兴隆台网站建设报价,网络营销,网络优化,兴隆台网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
---增,删,改,查
一:增(insert)
二:删(delete)
三:改(update)
四:查(select)
---连接数据库
[mongo@cjc bin]$ pwd
/usr/local/mongodb/bin
[mongo@cjc bin]$ ./mongo --port 27017
---创建新数据库cjcdb和cjctest
MongoDB Enterprise > use cjcdb
---插入数据,才会创建数据库
MongoDB Enterprise > db.tab01.insert({"time":"2018-11-25 18:20"})
MongoDB Enterprise > use cjctest
MongoDB Enterprise > db.tab01.insert({"time":"2018-11-25 18:21"})
---查看有哪些数据库
MongoDB Enterprise > show dbs
...
cjcdb 0.000GB
cjctest 0.000GB
---MongoDB 删除数据库cjctest
MongoDB Enterprise > use cjctest
switched to db cjctest
---查看当前连接的数据库
MongoDB Enterprise > db
cjctest
---删除数据库(谨慎操作)
MongoDB Enterprise > db.dropDatabase()
{ "dropped" : "cjctest", "ok" : 1 }
---MongoDB 创建集合
MongoDB Enterprise > use cjcdb
switched to db cjcdb
MongoDB Enterprise > db.createCollection("tab02")
{ "ok" : 1 }
MongoDB Enterprise > db.createCollection("tab03")
{ "ok" : 1 }
在 MongoDB 中,你不需要创建集合。当你插入一些文档时,MongoDB 会自动创建集合。
MongoDB Enterprise > db.tab05.insert({"id":"123456"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > show collections
tab01
tab02
tab03
tab05
......
================
一:增(insert)
================
插入文档
MongoDB 使用 insert() 或 save() 方法向集合中插入文档:
MongoDB Enterprise > use cjcdb
switched to db cjcdb
MongoDB Enterprise > db
cjcdb
MongoDB Enterprise >
db.tab02.insert({id:1, name:'a'})
db.tab02.insert({id:1, name:'ab'})
db.tab02.insert({id:1, name:'cae'})
db.tab02.insert({id:2, name:'b'})
db.tab02.insert({id:3, name:'c'})
db.tab02.insert({id:4, name:'d'})
db.tab02.insert({id:5, name:'e'})
db.tab02.insert({id:6, name:'f'})
db.tab02.insert({id:7, name:'g'})
db.tab02.insert({id:8, name:'h'})
db.tab02.insert({id:9, name:'i'})
db.tab02.insert({id:10, name:'j'})
db.tab02.insert({id:10, name:'h'})
db.tab02.insert({id:10, name:'i'})
db.tab02.insert({id:10,c100:'h',c200:'f',c300:'业精于勤'})
db.tab02.insert({ccc:10,c10:'h',c20:'f',c30:'行成于思'})
---我们也可以将数据定义为一个变量,如下所示:
MongoDB Enterprise > document=({title: 'blog.itpub',
... description: 'chenoracleblog-chenjuchao',
... by: 'mongo test',
... url: 'http://blog.itpub.net/29785807/'
... })
MongoDB Enterprise > db.tab02.insert(document)
=================
二:删(delete)
=================
---delete
MongoDB remove()函数是用来移除集合中的数据。
MongoDB数据更新可以使用update()函数。在执行remove()函数前先执行find()命令来判断执行的条件是否正确,这是一个比较好的习惯
MongoDB Enterprise > db
cjcdb
---删除tab02表中id=10的数据
MongoDB Enterprise > db.tab02.find({id:10})
{ "_id" : ObjectId("5bfacc790063f23910737472"), "id" : 10, "name" : "j" }
{ "_id" : ObjectId("5bfacc790063f23910737473"), "id" : 10, "name" : "h" }
{ "_id" : ObjectId("5bfacc790063f23910737474"), "id" : 10, "name" : "i" }
{ "_id" : ObjectId("5bfacc790063f23910737475"), "id" : 10, "c100" : "h", "c200" : "f", "c300" : "业精于勤" }
MongoDB Enterprise >
MongoDB Enterprise > db.tab02.remove({id:10})
WriteResult({ "nRemoved" : 4 })
---remove() 方法已经过时了,现在官方推荐使用 deleteOne() 和 deleteMany() 方法。
删除 id 等于 1 的第一个文档:
MongoDB Enterprise > db.tab02.find({id:1})
{ "_id" : ObjectId("5bfacc790063f23910737467"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("5bfacc790063f23910737468"), "id" : 1, "name" : "ab" }
{ "_id" : ObjectId("5bfacc790063f23910737469"), "id" : 1, "name" : "cae" }
MongoDB Enterprise > db.tab02.deleteOne({id:1})
{ "acknowledged" : true, "deletedCount" : 1 }
MongoDB Enterprise > db.tab02.find({id:1})
{ "_id" : ObjectId("5bfacc790063f23910737468"), "id" : 1, "name" : "ab" }
{ "_id" : ObjectId("5bfacc790063f23910737469"), "id" : 1, "name" : "cae" }
---删除 id 等于 1 的全部文档:
MongoDB Enterprise > db.tab02.deleteMany({id:1})
{ "acknowledged" : true, "deletedCount" : 2 }
MongoDB Enterprise > db.tab02.find({id:1})
MongoDB Enterprise >
---如删除集合下全部文档:
---truncate table ......
MongoDB Enterprise > db.tab02.find()
{ "_id" : ObjectId("5bfacc790063f2391073746a"), "id" : 2, "name" : "b" }
{ "_id" : ObjectId("5bfacc790063f2391073746b"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("5bfacc790063f2391073746c"), "id" : 4, "name" : "d" }
{ "_id" : ObjectId("5bfacc790063f2391073746d"), "id" : 5, "name" : "e" }
{ "_id" : ObjectId("5bfacc790063f2391073746e"), "id" : 6, "name" : "f" }
{ "_id" : ObjectId("5bfacc790063f2391073746f"), "id" : 7, "name" : "g" }
{ "_id" : ObjectId("5bfacc790063f23910737470"), "id" : 8, "name" : "h" }
{ "_id" : ObjectId("5bfacc790063f23910737471"), "id" : 9, "name" : "i" }
{ "_id" : ObjectId("5bfacc7a0063f23910737476"), "ccc" : 10, "c10" : "h", "c20" : "f", "c30" : "行成于思" }
{ "_id" : ObjectId("5bfaccb10063f23910737477"), "title" : "blog.itpub", "description" : "chenoracleblog", "by" : "mongo test", "url" : "http://blog.itpub.net/29785807/" }
MongoDB Enterprise > db.tab02.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 10 }
MongoDB Enterprise > db.tab02.find()
MongoDB Enterprise >
---删除集合
---drop table ......
MongoDB Enterprise > show collections
tab01
tab02
tab03
MongoDB Enterprise > db.tab02.drop()
true
MongoDB Enterprise > show collections
tab01
tab03
===============
三:改(update)
===============
---MongoDB 更新文档
MongoDB 使用 update() 和 save() 方法来更新集合中的文档。
MongoDB Enterprise > db.tab05.find({'id':1})
{ "_id" : ObjectId("5bfafab30063f23910737478"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("5bfafab30063f23910737479"), "id" : 1, "name" : "aa" }
{ "_id" : ObjectId("5bfafab30063f2391073747a"), "id" : 1, "name" : "aaa" }
---更改符合条件的第一条数据
MongoDB Enterprise > db.tab05.update({'id':1},{$set:{name:'chenjuchao'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.tab05.find({'id':1})
{ "_id" : ObjectId("5bfafab30063f23910737478"), "id" : 1, "name" : "chenjuchao" }
{ "_id" : ObjectId("5bfafab30063f23910737479"), "id" : 1, "name" : "aa" }
{ "_id" : ObjectId("5bfafab30063f2391073747a"), "id" : 1, "name" : "aaa" }
---multi:true更改符合条件的所有数据
MongoDB Enterprise > db.tab05.update({'id':1},{$set:{name:'chenjuchao'}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
MongoDB Enterprise > db.tab05.find({'id':1})
{ "_id" : ObjectId("5bfafab30063f23910737478"), "id" : 1, "name" : "chenjuchao" }
{ "_id" : ObjectId("5bfafab30063f23910737479"), "id" : 1, "name" : "chenjuchao" }
{ "_id" : ObjectId("5bfafab30063f2391073747a"), "id" : 1, "name" : "chenjuchao" }
================
四:查(select)
================
1 (!=)不等于 - $ne
2 (in)包含 - $in
3 (not in) - $nin 不包含
4 (>) 大于 - $gt
5 (>=) 大于等于 - $gte
6 (<) 小于 - $lt
7 (<= ) 小于等于 - $lte
8 模糊查询
9 AND
10 OR
11 Limit
12 Skip
13 Sort
14 count
---插入测试数据
---以易读的方式来读取数据,可以使用 pretty() 方法
MongoDB Enterprise>
db.tab01.insert({name:'a',age:'10'})
db.tab01.insert({name:'ab',age:'10'})
db.tab01.insert({name:'bac',age:'10'})
db.tab01.insert({name:'d',age:'12'})
db.tab01.insert({name:'e',age:'13'})
db.tab01.insert({name:'aaa',age:'14'})
db.tab01.insert({name:'f',age:'15'})
db.tab01.insert({name:'g',age:'16'})
db.tab01.insert({name:'a',age:'17'})
db.tab01.insert({name:'a',age:'18'})
db.tab01.insert({name:'a',age:19})
db.tab01.insert({name:'a',age:20})
---查看全部
MongoDB Enterprise> db.tab01.find()
---加pretty()更易读
db.tab01.find().pretty()
=====
等于
=====
MongoDB Enterprise> db.tab01.find({age:20})
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
MongoDB Enterprise> db.tab01.find({age:'20'})
MongoDB Enterprise>
MongoDB Enterprise> db.tab01.find({age:'10'})
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }
MongoDB Enterprise> db.tab01.find({age:10})
MongoDB Enterprise>
====================
1 (!=)不等于 - $ne
====================
MongoDB Enterprise> db.tab01.find({name:{$ne:'a'}});
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a9"), "name" : "e", "age" : "13" }
{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ab"), "name" : "f", "age" : "15" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }
{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }
{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }
==================
2 (in)包含 - $in
==================
MongoDB Enterprise> db.tab01.find({name:{$in:['cba','nba']}});
{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }
{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }
=========================
3 (not in) - $nin 不包含
=========================
MongoDB Enterprise> db.tab01.find({age:{$nin:['10','12','13','14','15',21]}});
{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
=================
4 (>) 大于 - $gt
=================
MongoDB Enterprise> db.tab01.find({age:{$gt:'17'}})
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
========================
5 (>=) 大于等于 - $gte
========================
MongoDB Enterprise> db.tab01.find({age:{$gte:'17'}})
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
========================
6 (<) 小于 - $lt
========================
MongoDB Enterprise> db.tab01.find({age:{$lt:'12'}})
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }
========================
7 (<= ) 小于等于 - $lte
========================
MongoDB Enterprise> db.tab01.find({age:{$lte:'12'}})
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }
============
8 模糊查询
============
---查询 name 包含"a"的文档:
MongoDB Enterprise> db.tab01.find({name:/a/})
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
---查询 name 字段以"a"开头的文档:
MongoDB Enterprise> db.tab01.find({name:/^a/})
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
---查询 name 字段以"a"结尾的文档:
---db.tab01.insert({name:'nba',age:21})
---db.tab01.insert({name:'cba',age:21})
MongoDB Enterprise> db.tab01.find({name:/a$/})
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }
{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }
=======
9 AND
=======
MongoDB 的 find() 方法可以传入多个键(key),每个键(key)以逗号隔开,即常规 SQL 的 AND 条件。
语法格式如下:
MongoDB Enterprise> db.tab01.find({name:'a', age:20})
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
MongoDB Enterprise> db.tab01.find({name:'a',age:{$gt:18}})
{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
=======
10 OR
=======
MongoDB OR 条件语句使用了关键字 $or:
MongoDB Enterprise> db.tab01.find({$or:[{name:'nba'},{age:'12'}]})
{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }
{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }
---AND 和 OR 联合使用
MongoDB Enterprise> db.tab01.find({name:'a',$or:[{age:'18'},{age:20}]})
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
==========
11 Limit
==========
MongoDB Limit() 方法
如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。
limit()方法基本语法如下所示:
MongoDB Enterprise> db.tab01.find({age:'10'}).limit(1)
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
MongoDB Enterprise> db.tab01.find().limit(2)
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }
=========
12 Skip
=========
MongoDB Skip() 方法
我们除了可以使用limit()方法来读取指定数量的数据外,还可以使用skip()方法来跳过指定数量的数据,skip方法同样接受一个数字参数作为跳过的记录条数。
注:skip()方法默认参数为 0 。
skip() 方法脚本语法格式如下:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
实例
以下实例只会显示第二条文档数据
MongoDB Enterprise> db.tab01.find({age:'10'}).limit(1).skip(2)
{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }
=======================
13 MongoDB 排序 Sort
=======================
skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit()。
MongoDB sort() 方法
在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
语法
sort()方法基本语法如下所示:
>db.COLLECTION_NAME.find().sort({KEY:1})
实例
---升序
MongoDB Enterprise> db.tab01.find().sort({age:1})
{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }
{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }
{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }
{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }
{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }
{ "_id" : ObjectId("5bfcc7b036850458dff055a9"), "name" : "e", "age" : "13" }
{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }
......
---降序
MongoDB Enterprise> db.tab01.find().sort({age:-1})
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }
......
---取age最大值
MongoDB Enterprise> db.tab01.find().sort({age:-1}).limit(1)
{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }
---取age最二大值
MongoDB Enterprise> db.tab01.find().sort({age:-1}).limit(1).skip(1)
{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }
---取age最三大值
MongoDB Enterprise> db.tab01.find().sort({age:-1}).limit(1).skip(2)
{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }
======================
14 count查询记录条数
======================
使用count()方法查询表中的记录条数,例如,下面的命令查询表users的记录数量:
MongoDB Enterprise> db.tab01.find().count();
14
欢迎关注我的微信公众号"IT小Chen",共同学习,共同成长!!!