//批量插入
for(i = 0;i<10;i++){
var sex = 'man';
if(i/2==0)
sex = 'women';
db.stu.insert({name:'name'+i,gender:sex,math:70+i})
}
//把name 值为zhagnsan的属性sex改为gender
db.stu.update({name:'zhangsan'},{$rename:{'sex':'gender'}},false,true)
//增加字段
db.stu.update({},{$set :{english:80}})
db.stu.update({},{$set:{chinese:90}},false,true)
//保存数组类型数据
db.stu.update({},{$set:{hobby:['baskball','football']}},false,true)
//指定查询列
db.stu.find({},{name:1,gender:1,hobby:1})
//指定不查询列
db.stu.find({},{name:0,gender:0,hobby:0})
//删除属性列sex
db.stu.update({},{$set:{sex:'man'}},false,true)
db.stu.update({},{$unset:{sex:1}},false,true)
//插入子文档
//一行
db.stu.update({},{$set:{contact:{qq:1234,phone:5678}}})
//多行
db.stu.update({},{$set:{contact:{qq:1234,phone:5678}}},false,true)
子文档查询
db.stu.find({'contact.qq':1234});
//多条件 and 查询 qq 1234 math 大于71
db.stu.find({'contact.qq':1234,'math':{$gt:71}});
//in
db.stu.find({'contact.qq':1234,'math':{$in:[71,72,73]}});
// not in
db.stu.find({'contact.qq':1234,'math':{$nin:[71,72,73]}});
//or
db.stu.find({$or:[{name:'zhangsan'},{gender:'women'}]})
db.stu.find({name:'name0',$or:[{name:'zhangsan'},{gender:'women'}]})
mongodb 简单操作
最新推荐文章于 2024-09-09 22:50:15 发布