解压版安装 cmd 执行
mongod.exe --dbpath "D:\Softwar\mongodb\data" --logpath "D:\Softwar\mongodb\log\mongodb.log" --install --serviceName "MongoDB" --logappend --directoryperdb
显示所有数据库
show dbs
使用数据库 没有则创建
use test
当前数据库 (db.getName(), db.stats() db.version(), db.getMongo())
db
显示数据库
show dbs
结构 database collection document
创建 collection (db.getCollection("users"), db.getCollections(), db.printCollectionStats())
创建之后不会显示, 必须插入一条数据才会显示这个collection
db.createCollection("users", {size: 20, capped: true, max: 100})
或者 插入一条数据,会自动创建一个 collection
db.users.save({name:"张三", age:18})
删除一个 collection 释放空间
db.users.remove() db.users.drop()
显示所有 collection
show collections
添加一条数据
db.users.save({key: value, key1: value1})
修改一条数据 前面第一个参数是where条件 (方法已过时)
修改 update users set key1 = new_value1 where key = value
db.users.update({key: value}, {$set: {key1: new_value1}}, false, true)
修改 update users set key1 = (value1 + new_value1) where key = value
db.users.update({key: value}, {$inc: {key1: new_value1}}, false, true);
修改 update users set key1 = (value1 + new_value1), key2 = new_value2 where key = value
db.users.update({key: value}, {$inc: {key1: new_value1}, $set: {key2: new_value2}}, false, true)
删除 delete from users where key = value
db.users.remove({key: value}) db.users.remove({key: value, key1: value1})
查询 select * from users
db.users.find()
去重查询 select distict key from users
db.users.distinct(key)
查询 select * from users where key = value
db.users.find({key: value})
查询 select * from users where key = value and key1 = value1
db.users.find({key: value, key1: value1})
查询 select * from users where age =18
db.users.find({age: 18})
查询 select * from users where age > 18
db.users.find({age: {$gt: 18}})
查询 select * from users where age >= 18
db.users.find({age: {$gte: 18}})
查询 select * from users where age < 60
db.users.find({age: {$lt: 60}})
查询 select * from users where age <= 60
db.users.find({age: {$lte: 60}})
查询 select * from users where age >= 18 and age < 60
db.users.find({age: {$gte: 18, $lt: 60}})
查询 select * from users where age < 18 or age >= 60
db.users.find({$or: [{age: {$lt: 18}}, {age: {$gte: 60}}]})
查询 name 和 age 两个字段 select name, age from users
db.users.find({}, {name: 1, age: 1})
查询 除 sex 字段的其他字段 select name, age from users
db.users.find({}, {sex: 0})
查询 select name, age from users where sex = '男'
db.users.find({sex: "男"}, {name: 1, age: 1})
升序 select * from users order by age asc
db.users.find().sort({age: 1})
降序 select * from users order by age desc
db.users.find().sort({age: -1})
查询前5条 select * from users limit 5
db.users.find().limit(5)
查询跳过前十条(limit 不支持子查询,使用临时表) select * from users where id not in (select id from (select id from users limit 1) temptable)
db.users.find().skip(10)
跳过前面 2条 查询5条 select * from users limit 5
db.users.find().skip(2).limit(5)
插入一条或多条
db.users.insertOne({name:"嬴政",age:88}) db.users.insertOne({name:"赵羽",age:24}) db.users.insertMany([ {name:"项燕",age:80}, {name:"项伯",age:69}, {name:"项梁",age:65}, {name:"项羽",age:22} ])
删除一条或多条
db.users.deleteOne({id:1}}) db.users.deleteMany({[{id:1}, {id:2}]})
删除所有 document
db.users.deleteMany({})
修改一条或多条 有点问题,看文档
db.users.updateOne({id:1},{$set:{name:"Robert"}}) db.users.updateMany([{id:1},{$set:{name:"Robert"}}])
查询 select * from users where name like '%项'
db.users.find({name:/^项/})
查询 select * from users where name like '羽%'
db.users.find({name:/羽$/})
查询 select * from users where name like '%羽%'
db.users.find({name:/羽/})
查询 排序 分页 示例
db.users.find().sort({age: -1}).limit(2) db.users.find().sort({age: -1}).skip(2).limit(2) db.users.find({name: /项/}).sort({age: -1}).skip(1).limit(2)
统计姓项的有几个
db.users.find({name:/^项/}).count()
找出姓项的年龄最小的
db.users.aggregate([ {$match:{name:/^项/}}, {$group:{_id: "min_age", age: {$min: "$age"}}} ]);
max min avg sum
db.getCollection('users').aggregate( { "$group":{_id: 'sum_age',value:{"$sum": "$age"}} } )
修改字段名
db.getCollection('集合名').update({}, {$rename : {"修改前字段名" : "修改后字段名"}}, {multi:true})