use database_name
show dbs
db
db.dropDatabase()
db.collection_Name.help();
show collections
db.createCollection(collection_Name)
db.collection_Name.insert(document)
db.runCommand({drop:'collection_Name'});
db.createCollection('logs',{size:50,max:5,capped:true});
db.runCommand({convertToCapped:"logs",size:5});
插入文档
db.collection_name.insert(document);
db.collection_name.save(document)
更新文档
db.collection.update(
,
,
{
upsert: ,
multi:
}
)
db.collection_Name.update({name:'xxx'},{$set:{yyy:10}},{multi:true});
更新操作符
db.collection_Name.update({name:'xxx'},{$set:{yyy:10}},{multi:true});
db.collection_Name.update({name:'xxx'},{$inc:{yyy:10}},{multi:true});
db.collection_Name.update({name:'xxx'},{$unset:{yyy:10}},{multi:true});
db.collection_Name.update({name:'xxx'},{
$push:{"hobbies":"smoking"}
});
db.collection_Name.update({name:'xxx',hobbies:{$ne:'smoking'}},{$push:{"hobbies":"smoking"}});
db.collection_Name.update({name:'xxx'},{$addToSet:{"hobbies":"smoking"}});
var hobbies = ["A",'B'];
db.collection_Name.update({name:'xxx'},{$addToSet:{hobbies:{$each:hobbies}}});
db.collection_Name.update({name:'xxx'},{$pop:{hobbies:1}});
db.collection_Name.update({name:'xxx'},{$set:{"hobbies.0":"smoking"}});
删除文档
db.collection.remove(
,
{
justOne:
}
)
db.collection_Name.remove({name:"xxx"},{justOne:true})
查询文档
db.collection_name.find()
db.collection_name.find({queryWhere},{key:1})
db.collection_Name.find({},{age:1});
db.collection_name.findOne()
db.collection_name.find({age:{$in:[30,100]}},{name:1,age:1});
db.collection_name.find({age:{$nin:[30,100]}},{name:1,age:1});
db.collection_name.find({age:{$not:{$gte:20,$lte:30}}});
db.collection_name.find({$where:"this.age>30"},{name:1,age:1});
db.runCommand({distinct:'collection_name',key:'key'}).values;
数组查询
db.collection_Name.find({friends:[ "A", "B", "C", "D" ]});
db.collection_Name.find({friends:"A"});
db.collection_Name.find({friends:{$all:['A','B']}});
db.collection_Name.find({friends:{$in:['A','B']}});
db.collection_Name.find({friends:{$size:4}});
db.collection_Name.find({},{friends:{$slice:[0,3]}});
"friends" : [ "A", "B", "C" ]
条件操作符
db.collectoin_name.find({:{$gt:}})
db.collectoin_name.find({:{$gte:}})
db.collectoin_name.find( {:{$lt:}})
db.collectoin_name.find({:{$lte:}})
db.collectoin_name.find({:{$gte:},:{$lte:}})
db.collection_name.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
)
db.collection_name.find(
{
key1:value1,
key2:value2,
$or: [
{key1: value1},
{key2:value2}
]
}
)
db.collectoin_name.find({"_id" : ObjectId("value")})
db.collectoin_name.find().count()
db.collection.find({key:/value/})
分页查询
db.collectoin_name.find().limit(number)
db.collectoin_name.find().skip(number)
db.collectoin_name.find().skip(skipNum).limit(limitNum)
db.collectoin_name.find().sort({key:1})
db.collectoin_name.find().sort({key:-1})
db.collection_Name.ensureIndex({key:1});
db.collection_Name.ensureIndex({key:1},{name:'keyIndex'});
db.collection_Name.getIndexes()//查看索引
db.collection_Name.find({key:'findcondition'}).explain();
db.collection_Name.find({key:'findcondition'}).hint("keyIndex").explain(true);
db.collection_Name.dropIndex('keyIndex');//删除指定的索引
db.collection_Name.dropIndex('*');
db.runCommand({dropIndexes:"collection_Name",index:"keyIndex"});//删除所有的索引
db.collection_Name.ensureIndex({key:1},{name:'keyIndex',unique:true,background:true});
db.collection_Name.ensureIndex({key1:1,key2:1});
db.collection_Name.insert({time:new Date()});
db.collection_Name.ensureIndex({time:1},{expireAfterSeconds:10});
db.article.find({$text:{$search:'boy'}});
db.article.find({$text:{$search:'girl'}});
db.article.find({$text:{$search:'boy girl'}});//多次查找,多个关键字为或的关系
db.article.find({$text:{$search:"a b"}});
db.article.find({$text:{$search:"boy -girl"}}); // -表示取消
db.article.find({$text:{$search:"a \"coco cola\" b "}}); //支持转义符的,用\斜杠来转义
db.runCommand({fsync:1,lock:1});
db.fsyncUnlock();
mongodump
-- host 127.0.0.1
-- port 27017
-- out D:/databack/backup
-- collection mycollection
-- db test
-- username
-- password
mongorestore
--host
--port
--username
--password
path
角色
插入用户
//use admin进入admin库,往里面插入用户
db.system.user.insert({
user:'xxx',
pwd:'123',
roles:[
{
role:'readWrite',
db:'dbName'
}
]
})
//或者调用createUser方法
db.createUser(
{
user: "",
pwd: "",
customData: { },
roles: [
{ role: "", db: "" } | "",
...
],
authenticationRestrictions: [
{
clientSource: ["" | "", ...]
serverAddress: ["" | "", ...]
},
...
]
}
)
权限登录
mongod --auth //或者在配置文件中配置auth=true
mongo -u username -p password 127.0.0.1:27017/dbName //客户端登录方式或者切换到数据库后使用下面鉴权
鉴权
use dbName
db.auth('username',password)
查看权限
db.runCommand({usersInfo:'username',showPrivileges:true});
修改密码
db.changeUserPassword(username, password)
增加角色
db.grantRolesToUser( "userName" , [ { role: "", db: "" } ])
移除角色
db.revokeRolesFromUser( "userName" , [ { role: "", db: "" } ])
更新角色
该方法必须运行在角色的数据库上。该方法会完全覆盖原来的角色配置。
db.updateRole(rolename, update, writeConcern)
删除用户
删除某个数据库的用户,需要先切换到该数据库,否则删除不成功。
db.dropUser(username)