db.person.find()
db.person.remove()
db.person.drop()//速度更快但不能指定限制条件
//定义方法
function f(n){
if(n<=2){
return 1
}else{
return f(n-2) + f(n-1)
}
}
f(4)
//替换操作
"wangzs , hello word!".replace("word","xxxxx");
person={"name":"wangzs2","age":27,"address":"xujingdong","date":new Date()};
db.person.insert(person);
db.person.findOne()
//更新
db.person.update({"name":"wangzs2"},{"$set":{"comments":[]}})
//插入的数据类型
db.person.insert({"a":"","b":null,"c":2,"d":3.12,"e":true,"f":NumberInt(8),"g":NumberLong(9),
"h":"hello","i":new Date(),"j":["1","2","3"],"k":{"name":"wangzs","age":"27"}})
//帮助命令
db.help()
db.person.help()
sh.help()
show dbs
show collections
show users
//查看函数实现代码,参数顺序
db.person.update
//批量插入
db.person.insertMany([{"name":"name1"},{"name":"name2"},{"name":"xxxx"}])//无法执行
db.person.insert({"name":"name1","age":25,"address":"上海"})
//把age的值加1
db.person.update({"name":"name1"},{"$inc":{"age":1}})
//更新
db.person.update({"name":"name1"},{"$set":{"books":"java"}})
db.person.update({"name":"name1"},{"$set":{"books":["mongodb","javascript","hello"]}})
//删除不要字段
db.person.update({"name":"name1"},{"$unset":{"books":1}})
//comments添加内容
db.person.update({"name":"wangzs"},{"$push":{"comments":{"星级":"*****","评论":"五星好评"}}})
db.person.update({"name":"name1"},{"$push":{"address":{"$each":[23,"88","youxiu"]}}})
//$addToSet避免插入重复地址
db.person.update({"name":"name1"},{"$addToSet":{"address":{"$each":[23,"youxiu","charu","hello"]}}})
db.person.insert({"todo":["A","B","C","A","A"]})
//删除数组中所有匹配的结果
db.person.update({},{"$pull":{"todo":"A"}})
//第三个参数ture,存在更新,如果不存在就创建
db.person.update({"name":"wangzs5"},{"$inc":{"age":3}},true)
//第4个参数true更新所有匹配的文档
db.person.update({"name":"wangzs"},{"$set":{"gift":"happy bitthday"}},false,true)
var person=db.person.find({"name" : "wangzs"})
person.name="wangss"
//包含_id更新,不包含_id保存
db.person.save(person)//运行有问题
//查找并且更新 只是1个
db.runCommand({"findAndModify":"person",
"query":{"name":"wangzs"},
"update":{"$set":{"address":"zhoukou"}}
}).value
//查找并且删除 只是1个
db.runCommand({"findAndModify":"person",
"query":{"name":"wangzs"},
"remove":true
}).value
//指定_id不显示,只显示name,address
db.person.find({},{"_id":0,"name":1,"address":1})
// 18<=age<=30
db.person.find({"age":{"$gte":18,"$lte":30}})
// name!=wangzs2
db.person.find({"name":{"$ne":"wangzs2"}})
// name in ("wangzs","wangzs3",123)
db.person.find({"name":{"$in":["wangzs","wangzs3",123]}})
// name not in ("wangzs","wangzs3",123)
db.person.find({"name":{"$nin":["wangzs","wangzs3",123]}})
// or
db.person.find({"$or":[{"age":30},{"name":{"$nin":["wangzs","wangzs3",123]}}]})
//返回parents=null和不包括返回parents字段的所有结果
db.person.find({"parents":null})
//返回包括parents字段但是该字段是null的结果 $exists:判断键值存在
db.person.find({"parents":{"$in":[null],"$exists":true}})
//正则表达式 模糊匹配*wangzs*
db.person.find({"name":/wangzs/})
db.person.insert({"name":["A","B","C"]})
db.person.insert({"name":["E","K","C"]})
db.person.insert({"name":["U","D","C","D","W"]})
//name数组包含A和C
db.person.find({"name":{"$all":["A","C"]}})
//name数组长度为3的结果
db.person.find({"name":{"$size":3}})
//name数组前2个位置
db.person.find({},{"name":{"$slice":2}})
//name数组后2个位置
db.person.find({},{"name":{"$slice":-2}})
//name数组后3-5位置
db.person.find({},{"name":{"$slice":[3,5]}})
db.person.insert({"name":"wangzs","address":[{"city":"shanghai","tel":"135"},{"city":"henan","tel":"888"}]})
//显示两条结果
db.person.find({"address.city":"shanghai","address.tel":"888"})
//elemMatch将限定条件进行分组
db.person.find({"address":{"$elemMatch":{"city":"shanghai","tel":"888"}}})
//遍历结果
var cursor=db.person.find()
while(cursor.hasNext){
obj = cursor.next()
print(obj)
}
var cursor=db.person.find()
cursor.forEach(function(x){
print(x.A+" - " +x.C)
})
//限制返回2条结果
db.person.find().limit(2)
//跳过前2条结果
db.person.find().skip(2)
//排序 1:升序 2:降序
db.person.find().sort({"A":1,"C":-1})
//查看所有数据库命令
db.listCommands()
//explain 显示执行查询过程中做的事情
db.person.find({"name":"name221"}).explain()
db.person.find({"name":"name221"}).limit(1).explain()
//name建立索引
db.person.ensureIndex({"name":1})
//name,age建立索引
db.person.ensureIndex({"name":1,"age":1})
//建立唯一索引
db.person.ensureIndex({"name":1},{"unique":true})
//删除索引
db.person.dropIndex({"name":1})
db.person.getIndexes() //无法执行
//查看索引创建的进度
db.currentOp()
//穿件唯一索引,重复的删除
db.person.ensureIndex({"name":1},{"unique":true,"dropDups":true}) //无法执行
//聚合函数
db.person.aggregate(
{"$match":{"name":"name5"}},// 对文档集合进行筛选
{"$project":{"name":1}}, // 选择需要投射的字段
{"$group":{"_id":"$name","count":{"$sum":1}}},
{"$sort":{"count":-1}},
{"$limit":5}
)
//按照多个字段分组
db.person.aggregate(
{"$group":{"_id":{"name":"$name","age":"$age"},"count":{"$sum":1}}},
{"$sort":{"count":-1}}
)
//分组后找到最大值最小值
db.person.aggregate(
{"$group":{"_id":"$name","lowage":{"$min":"$age"},"highage":{"$max":"$age"}}}
)
//把id重命名为userid,不显示_id,显示name
db.person.aggregate(
{"$project":{"userid":"$_id","_id":0,"name":1}}
)
db.person.insert({"name":"wangzs1","num1":12,"num2":5})
db.person.insert({"name":"wangzs2","num1":8,"num2":9})
//把num1和num2的值相加
db.person.aggregate(
{"$project":
{"result":
{"$add":["$num1","$num2"]}
}
}
)
db.person.insert({"name":"wangzs2","hireDate":new Date()})
db.person.insert({"name":"wangzs2","hireDate":new Date("2016/08/07")})
db.person.insert({"name":"wangzs2","hireDate":new Date("2016/05/07")})
//返回每个雇员入职的月份
db.person.aggregate(
{"$project":{"hiredIn":{"$month":"$hireDate"}}}
)
//返回员工在公司工作的月数
db.person.aggregate(
{"$project":{"workTime":
{"$subtract":[{"$month":new Date()},{"$month":"$hireDate"}]}
}}
)
db.person.insert({"name":"wangzs2","address":"henan"})
//取name前两位拼接邮箱号
db.person.aggregate({
"$project":
{"email":
{"$concat":[{"$substr":["$name",0,2]},".","$address",".","dongh.com"]}
}
})
db.person.count({"name":"wangzs2"})
//person集合中name的不同数量
db.person.runCommand({"distinct":"person","key":"name"})
mongoDB常用命令
最新推荐文章于 2019-11-14 03:51:09 发布