MongoDB - 数据库常用命令
use [数据库名]
show dbs
db / db.getName()
db.stats()
db.version()
db.getMongo()
db.dropDatabase()
MongoDB - 集合常用命令
db.createCollection('集合名字');
db.createCollection('集合名字', { capped: true, size: 20, max: 100 })
db.collName.isCapped(); // 判断集合是否为定容量集合
db.getCollection(name);
db.getCollectionNames();
db.printCollectionStats()
MongoDB - 集合常用命令 - 添加
- save
- insertOne
- insertMany
db.users.save({name: '张三', age: 16});
db.users.insertOne({name: '张三', age: 18});
db.users.insertMany([{name: '李四', age: 19}, {name: '王五', age: 20}]);
MongoDB - 集合常用命令 - 修改
- save
传入 _id 时,可作为更新操作。
- update
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
}
)
// query: update的查询条件,类似sql update查询内where后面的。
// update: update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
// upsert: 可选,如果未找到,是否允许插入一条记录,默认为 false。
// multi: 可选,是否更新多条记录,默认为 false。
db.users.update({age: 25}, {$set: {name: 'changeName'}});
// update users set name = 'changeName' where age = 25;
db.users.update({name: 'lisi'}, {$inc: {age: 50}});
// update users set age = age + 50 where name = 'lisi';
db.users.update({name: 'lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}});
// update users set age = age + 50, name = 'hoho' where name = 'lisi';
MongoDB - 集合常用命令 - 删除
db.collection.remove(
<query>,
{
justOne: <boolean>
}
)
// query: 条件
// justOne: 是否只删除一条,默认为 false 。
MongoDB - 集合常用命令 - 查询
db.users.find()
// select * from users
db.users.find({'age': 22});
// select * from users where age = 22;
db.users.find({'age': {$gt: 22}});
// select * from users where age > 22;
db.users.find({'age': {$lt: 22}});
// select * from users where age < 22;
db.users.find({'age': {$gte: 22}});
// select * from users where age >= 22;
db.users.find({'age': {$lte: 22}});
// select * from users where age <= 22;
db.users.find({'age': {$ne: 22}});
// select * from users where age != 22;
- 查询 age >= 23 并且 age <= 26
db.users.find({'age': {$gte: 23, $lte: 26}});
- 查询 age >= 23 或者 name == '张三'
db.users.find({$or: [{age: {$gte: 23}}, {name: '张三'}]);
db.users.find({'name': /mongo/});
// select * from users where name like %mongo%;
db.users.find({'name': /^mongo/});
// select * from users where name like 'mongo%';
db.users.find({'name': /mongo$/});
// select * from users where name like 'mongo%';
db.users.find({}, {name: 1, age: 1});
// select name, age from users;
- 查询 指定列 name 、age 并且 age > 25
db.users.find({age: {$gt: 25}}, {name: 1, age: 1});
// select name, age from users where age > 25;
db.users.find().sort({age: 1});
db.users.find().sort({age: -1});
- 查询 name = zhangsan , age = 20 的数据
db.users.find({name: '张三', age: 20});
// select * from users where name='zhangsan' and age='20';
db.users.find().limit(5);
// select top 5 * from users;
db.users.find().skip(10);
db.usres.find().limit(10).skip(5);
db.users.findOne();
db.users.find().count();
MongoDB - 集合常用命令 - 查询修改删除
db.users.findAndModify({
query: {age: {$gte: 25}},
sort: {age: -1},
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
})
pic9.jpg
网友评论