1 MongoDb实用中常用命令与mysql对照理解
2 Mongo自带性能统计工具mongostatus
1 show dbs 查询有的数据库
> show dbs
admin 0.000GB
apt_trace 0.001GB
local 0.000GB
test1 0.000GB
>bd #显示当前数据库
test1
2 use DATABASE_NAME 创建数据库(如果存在就切换到数据库)
3 删除数据库
>use test1
> db.dropDatabase()
{ "dropped" : "test1", "ok" : 1 }
4 查询xx库中的集合(集合相对于mysql中的表)
> show collections
apt_actor
apt_ioc
apt_report
apt_toolset
> db.mytest.insert({'name':'test'})
WriteResult({ "nInserted" : 1 })
>
> show collections
apt_actor
apt_ioc
apt_report
apt_toolset
mytest
> db.mytest.drop()
true
>db.apt_actor.update({'name':'test','aaa':'bbbb'},{'name':'test','aaa':'bbbbcccccccc','fffffffff':'123123'}) # 更新update文档
> show collections
apt_actor
apt_report
apt_toolset
5 文档(相对于mysql中的行)
> db.test.insert({title: 'MongoDB 教程2', description: 'MongoDB', by: 'test', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5d5a73f186ece27702e537db"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "test", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
6 更新文档,文档中数据内容的更新
> db.test.find()
{ "_id" : ObjectId("5d5a73f186ece27702e537db"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "test", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5d5a742886ece27702e537dc"), "title" : "MongoDB 教程2", "description" : "MongoDB", "by" : "test", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>
>
> db.test.update({'title':'MongoDB 教程2'},{$set:{'title':'22222222222'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
>
> db.test.find()
{ "_id" : ObjectId("5d5a73f186ece27702e537db"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "test", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5d5a742886ece27702e537dc"), "title" : "22222222222", "description" : "MongoDB", "by" : "test", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>
> db.test.remove({'title':'22222222222'})
WriteResult({ "nRemoved" : 1 })
7 格式化显示数据
> db.test.find().pretty()
{
"_id" : ObjectId("5d5a73f186ece27702e537db"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "test",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
> db.test.find().pretty().limit(1) #limit参数
{ "_id" : ObjectId("5d5a7f94859f034e059da6c2"), "name" : "test" }
>
> db.test.find().pretty().limit(1).skip(2) #skip参数跳过指定数量的参数
{ "_id" : ObjectId("5d5a7fc0859f034e059da6c4"), "name" : "test2" }
> db.test.find().pretty().sort({'name':-1}) #以name字段降序排列
{ "_id" : ObjectId("5d5a7fc0859f034e059da6c4"), "name" : "test2" }
{ "_id" : ObjectId("5d5a7fbe859f034e059da6c3"), "name" : "test1" }
{ "_id" : ObjectId("5d5a7f94859f034e059da6c2"), "name" : "test" }
>
>
>
> db.test.find().pretty().sort({'name':1}) #以name字段升排序
{ "_id" : ObjectId("5d5a7f94859f034e059da6c2"), "name" : "test" }
{ "_id" : ObjectId("5d5a7fbe859f034e059da6c3"), "name" : "test1" }
{ "_id" : ObjectId("5d5a7fc0859f034e059da6c4"), "name" : "test2" }
>
>db.col.find({likes : {$gt : 100}}) #条件查询,类似Select * from col where likes > 100;
>db.apt_report.find({'insert_time':{$gt : '2019-08-21 11:07:52'}}).pretty().count()
>db.apt_report.find({'insert_time':{$gt : '2019-08-21 11:07:52'}}).pretty().count() #删除大于该时间戳的数据
>db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) #该查询操作返回 inventory 集合中,qty 值是 5 或 15 的所有文档
8 聚合函数,类似于mysql的group by 命令(做数据统计和计数使用)
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
9 mongo监控 mongostatus | mongotop
10 正则表达查询
db.test.find({'name':{$regex:"st"}}) # 查询name字段中包含‘st’
mongostatus统计实例:
status统计各字段说明参考该博客,写的非常简明
https://blog.csdn.net/u011186019/article/details/70918288