昊鼎王五:Mongodb常用命令你用过哪些?

版权声明:感谢您阅读我的文章,转载注明出处哦~~ https://blog.csdn.net/haoding205/article/details/82384856

昊鼎王五:Mongodb常用命令你用过哪些?

1.Mongodb基本命令

1.1.数据库相关

例: use foobar

1.1.1.建表:

例:db.persons.insert({name:’Tom’,sex:’boy’,age:19,country:’USA’})

1.1.2.查询

例:db.persons.find();

1.1.3.更新

例: db.persons.update({name:’Tom’},{$set:{age:20}})

1.1.4.删除

例:db.persons.remove({age: 18});

1.2.查询例子

1.2.1.会过滤掉name中的相同数据

db.persons.distinct("name");
相当于:select distict name from persons;

1.2.2.查询age > 22的记录

db.persons.find({age: {$gt: 22}});
相当于:select * from persons where age >22;

1.2.3.查询age >= 25的记录

db.persons.find({age: {$gte: 25}});
相当于:select * from persons where age >= 25;

1.2.4.查询name中包含 li的数据

db.persons.find({name: /li/});
//相当于%%
[code]select * from persons where name like ‘%li%';

1.2.5.查询name中以li开头的

db.persons.find({name: /^li/});
相当于 #select * from persons where name like ‘li%';

1.2.6.查询指定列name、age数据, age > 25

db.persons.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from persons where age >25;

1.2.7.按照年龄排序

升序:db.persons.find().sort({age: 1});
降序:db.persons.find().sort({age: -1});

1.2.8.查询前5条数据

db.persons.find().limit(5);
相当于:select top 5 * from persons;

1.2.9.查询5条以后的数据

db.persons.find().skip(5);
相当于:select * from persons where id not in (
selecttop 5* from persons
);

1.2.10.查询在5-10之间的数据

db.persons.find().limit(10).skip(5);
可用于分页,limit是pageSize,skip是第几页*pageSize

1.2.11.or与 查询

db.persons.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from persons where age = 22 or age = 25;

1.2.12.or与and 复合 查询

#复合查询
db.getCollection('publishStatusLog').find( {
    $and : [
        { $or : [ { "status":"start" }, { "status":"success" } ] },
        { "projectVo.projName":new RegExp("昊鼎") }
    ]
} )
db.getCollection('publishStatusLog').find({"taskId":"15245590823773"})

1.2.12.正则表达式 查询

$lt:小于,
$lte:小于等于,
$gt:大于,
$gte:大于等于
#查询条件中包含like时,格式为:{"operParam5":new RegExp(".*set.*")}
#这里使用到简单的正则表达式:.*表示任意多个字符,相当于like中的%。
#如下图

mongodb正则表达式

1.2.13.模糊 查询

db.fs.files.find({ "filename" : { "$regex" : "^.*/index.html" , "$options" : "i"}}).count()/2;

2.索引

2.1、创建索引

db.persons.ensureIndex({name: 1});
db.persons.ensureIndex({name: 1, age: -1});
#注:   1是升序排列 , -1是倒序排列

2.2、查询当前聚集集合所有索引

db.persons .getIndexes();

2.3、查看总索引记录大小

db.persons .totalIndexSize();

2.4、读取当前集合的所有index信息

db.users.reIndex();

2.5、删除索引

db.users.dropIndexes();

3.删除集合数据

db.persons .findAndModify({
    query: {age: {$gte: 25}}, 
    sort: {age: -1}, 
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
});
db.runCommand({ findandmodify : "persons ", 
    query: {age: {$gte: 25}}, 
    sort: {age: -1}, 
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
});

4.循环添加数据

> for (var i = 0; i < 30; i++) {
     db.persons .save({name: "u_" + i, age: 22 + i, sex: i % 2});
  };

5.停止与启动

5.1.关闭进程

停止 MongoDB时
不要使用 kill -9去关闭mongod !这样数据库会不理一切直接杀死该进程,会使得数据文件损坏。
稳妥的方法是使用 kill -2 pid去关闭mongod ,也就是当 mongod进程接受到关闭指令后会等待当前运行操作或文件分配等操作完毕后,关闭所有打开的连接,并将缓存的数据刷新到磁盘后才正式关闭。
最稳妥的方式是使用 shutdown命令来结束
#稳妥方法1:
> use admin
switched to db admin
> db.shutdownServer();
#稳妥方法2:
> use admin
> db.adminCommand('shutdown':1)
#稳妥方法3关闭mongos
mongo  -u clusterAdmin -p password 127.0.0.1:30000/admin --eval 'db.shutdownServer()'

5.2.后台启动

# 使用&后台启动
./bin/mongod -port 10001 --dbpath ../data/ --logpath ../log/mongodb.log &
# --fork 选项将会通知 mongod 在后台运行
./bin/mongod -port 10001 --dbpath ../data/ --logpath ../log/mongodb.log --fork

好了,聪明如你,知道了Mongodb常用命令,是不是很欢喜 ^_^

还有其他问题的可以在评论区留言或者扫码加博主获取资源或者提问。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/haoding205/article/details/82384856