(测试数据库:testdb 集合:person , animal 属性:name , age)
- show dbs
- use testdb
- show collections
- db.person.find()
- db.createCollection('animal')
- db.animal.insert({'name':'dog',age:2})
- db.animal.drop()
- db.dropDatabase()
- db.person.insertOne({'name':'zhangsan','age':20})
- db.person.insertOne({'_id':8,'name':'zhangsan','age':30})
- db.person.insertMany([{'name':'lisi','age':25},{'name':'wangwu','age':26}])
-
-
1.删除单篇文档
- db.person.deleteOne({'name':'lisi'})
-
2.删除多篇文档
- db.person.deleteMany({'name':'lisi'})
-
-
-
1.修改单篇文档
- db.person.updateOne({'name':'lisi'},{$set:{'age':30}})
如果有age那列没有,将文档增加一列,如果有就修改 - db.person.updateOne({'name':'lisi'},{$set:{'age':30}},{upsert:true})
如果有name='lisi’的文档,将被修改,如果没有,将添加此新文档 - db.person.updateOne({'name':'lisi'},{$rename:{'age':'address'}})
$rename 重命名某个列 - db.person.updateOne({'name':'lisi'},{$unset:{'age':''}})
$unset 删除某个列
- db.person.updateOne({'name':'lisi'},{$set:{'age':30}})
-
2.修改多篇文档
- db.person.updateMany({'name':'lisi'},{$set:{'age':40}})
-
- db.person.find()
- db.person.find({},{'name':1,'address':1})
- db.person.find({'name':'lisi'})
- db.person.find({'name':{$eq:'lisi'}})
- db.person.find({'name':{$ne:'lisi'}})
- db.person.find({'age':{$gt:22}})
- db.person.find({'age':{$gte:20}})
- db.person.find({'age':{$lt:22}})
- db.person.find({'age':{$lte:20}})
-
db.person.find({'age':{$in:[20,25]}})
db.person.find({'name':{$in:['zhangsan','lisi']}}) -
db.person.find({'age':{$nin:[20,25]}})
db.person.find({'name':{$nin:['lisi']}}) -
db.person.insert({'name':'lisi','city':['gd','gx']})
db.person.find({'city':{$all:['gd','gx']}}) - db.person.find({'city':{ $exists:1}})
- db.person.find({$nor:[{age:20},{age:25}]})
- db.person.find({'name':/li*/})
- db.person.find({"$where" : "this.age>20"}).pretty()
-
var cursor = db.person.find() while(cursor.hasNext()){printjson(cursor.next());}
-
for(var cursor=db.person.find(), doc=true;cursor.hasNext();) {printjson(cursor.next());}
-
var getname = function(obj) {print(obj.name)}
var cursor = db.person.find()
cursor.forEach(getname) -
var cursor = db.person.find().skip(2).limit(2);
for(doc=true;cursor.hasNext();) { printjson(cursor.next());} -
var cursor = db.person.find()
printjson(cursor.toArray())
printjson(cursor.toArray()[2])
- db.person.find().explain()
- db.person.getIndexes()
- db.person.ensureIndex({age:-1})
- db.person.ensureIndex({'name':1},{unique:true});
- db.person.dropIndex({age:-1})
- db.person.dropIndexes()
- db.person.insert({'name':'lisi','address':{'city':'hn'}}) db.person.ensureIndex({'address.city':1})
- db.person.dropIndex({'address.city':1})
- db.person.ensureIndex({'name':1},{sparse:true})
- db.person.ensureIndex({'name':'hashed'})
-
db.person.insertMany([{'name':'lisi','age':25,'number':10},{'name':'lisi','age':26,'number':20}])
db.person.group(
{
key:{'number':1},
cond:{},
reduce:function(curr,result) {
result.cnt += 1;
},
initial:{cnt:0}
}
) -
db.person.insertMany([{'name':'lisi','age':25,'number':10},{'name':'lisi','age':26,'number':20}])
db.person.group(
{
key:{'name':1},
cond:{},
reduce:function(curr,result) {
result.cnt += 1;
},
initial:{cnt:0}
}
) -
db.person.insertMany([{'name':'lisi','age':25,'number':10},{'name':'lisi','age':26,'number':20}])
db.person.group(
{
key:{'name':1},
cond:{'age':{$gt:25}},
reduce:function(curr,result) {
result.cnt += 1;
},
initial:{cnt:0}
}
) -
db.person.group(
{
key:{'name':1},
cond:{},
reduce:function(curr,result) {
result.num += curr.number;
},
initial:{num:0}
}
) -
db.person.group(
{
key:{'name':1},
cond:{},
reduce:function(curr , result) {
if(curr.number > result.max) {
result.max = curr.number;
}
},
initial:{max:0}
}
) -
db.person.group(
{
key:{'name':1},
cond:{},
reduce:function(curr , result) {
result.cnt += 1;
result.sum += curr.number;
},
initial:{sum:0,cnt:0},
finalize:function(result) {
result.avg = result.sum/result.cnt;
}
}
)
-
db.person.aggregate(
[{$group:{_id:"$name",total:{$sum:1}}}]
) -
db.person.aggregate(
[{$group:{_id:null,total:{$sum:1}}}]
) -
db.person.aggregate(
[
{$match:{number:{$gt:10}}},
{$group:{_id:"$name",total:{$sum:1}}}
]
) -
db.person.aggregate(
[
{$match:{number:{$gt:10}}},
{$group:{_id:"$name",total:{$sum:1}}},
{$match:{total:{$gte:3}}}
]
) -
db.person.aggregate(
[
{$group:{_id:"$name",total:{$sum:"$number"}}},
{$sort:{total:1}}
]
) -
db.person.aggregate(
[
{$group:{_id:"$name",avg:{$avg:"$number"}}},
{$sort:{avg:-1}}
]
)
-
var map = function() {
emit(this.name,this.number);
}
var reduce = function(name,numbers) {
return Array.sum(numbers);
}
db.person.mapReduce(map,reduce,{out:'res'});
db.res.find() -
var map = function() {
emit(this.name,this.number);
}
var reduce = function(name,values) {
return Array.avg(values);
}
db.person.mapReduce(map,reduce,{out:'res'});
db.res.find()