本篇文章介绍学习MongoDB的一些常用命令,希望能帮助大家.
Help
查看命令提示db.help()
;db.yourColl.help()
;db.youColl.find().help()
;use yourDB
; 当创建一个集合(table
)的时候会自动创建当前数据库show dbs
;db.dropDatabase()
;db.copyDatabase("mydb", "temp", "127.0.0.1")
;将本机的mydb
的数据复制到temp
数据库中db.repairDatabase()
;db.getName()
;db.stats()
;db.version()
;db.getMongo()
;table
)db.createCollection(“collName”, {size: 20, capped: 5, max: 100})
;table
)db.getCollection("account")
;db.getCollectionNames()
;db.printCollectionStats()
;db.addUser("name")
;db.addUser("userName", "pwd123", true)
; 添加用户、设置密码、是否只读db.auth("userName", "123123")
;show users
;db.removeUser("userName")
;db.userInfo.find()
; 相当于:select* from userInfo
;默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize= 50;这样每页就显示50条记录了。db.userInfo.distinct("name")
;会过滤掉name中的相同数据,相当于:select distict name from userInfo
;age = 22
的记录db.userInfo.find({"age": 22})
; 相当于:select * from userInfo where age = 22
;age > 22
的记录db.userInfo.find({age: {$gt: 22}})
;age < 22
的记录db.userInfo.find({age: {$lt: 22}})
;age >= 25
的记录db.userInfo.find({age: {$gte: 25}})
;age >= 23
并且 age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}})
;name
中包含 mongo
的数据db.userInfo.find({name: /mongo/})
;name
中以mongo
开头的db.userInfo.find({name: /^mongo/})
;name
、age
数据db.userInfo.find({}, {name: 1, age: 1})
;当然name
也可以用true
或false
,当用ture
的情况下河name:1
效果一样,如果用false
就是排除name
,显示name
以外的列信息。name
、age
数据, age > 25
.db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1})
;db.userInfo.find().sort({age: 1})
;db.userInfo.find().sort({age: -1})
;name = zhangsan, age = 22
的数据db.userInfo.find({name: 'zhangsan', age: 22})
;db.userInfo.find().limit(5)
;db.userInfo.find().skip(10)
;db.userInfo.find().limit(10).skip(5)
;可用于分页,limit是pageSize,skip是第几页*pageSizedb.userInfo.find({$or: [{age: 22}, {age: 25}]})
;db.userInfo.findOne()
;db.userInfo.find().limit(1)
;db.userInfo.find({age: {$gte: 25}}).count()
;如果要返回限制之后的记录数量,要使用count(true)或者count(非0)db.users.find().skip(10).limit(5).count(true)
;db.userInfo.find({sex: {$exists: true}}).count()
;db.userInfo.ensureIndex({name: 1})
;db.userInfo.ensureIndex({name: 1, ts: -1})
;db.userInfo.getIndexes()
;db.userInfo.totalIndexSize()
;db.users.reIndex()
;db.users.dropIndex("name_1")
;db.users.dropIndexes()
;扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有