首页 > 代码库 > mongodb操作记录

mongodb操作记录

[User]
1.db.addUser("name","pwd","true/false")
2.db.auth("name","pwd")
3.show users
4.db.removeUser("name")

[Collection]
1:db.createCollection("name")
2:db.getCollectionNames()

[Document]
1:db.User.save({})
2:db.User.find()
3:db.User.find({})
4:db.User.find({}).sort({})
5:db.User.find().skip().limit()
6:db.User.find({age:{$gt:23,$lt:32}})
$gt/$lt/$gte/$lte/$ne
7:db.User.find({age:{$in:[12,13,14]}})
8:db.User.find({$or:[{age:{$lte:23}},{age:{$gte:33}}]})
9:db.User.update({name:‘name1‘},{$set:{age:24,sex:false}})
10:db.User.update({条件},{更新表达式},如果记录不存在是否插入,是否更新多个文档)
11:db.User.remove(id)
12:db.User.remove()

13:db.User.distinct(‘name‘)
14:Group
db.User.group(
{
key:{‘name‘:true},
cond:{‘name‘:‘name1‘},
reduce:function(obj,prev){
prev.msum += obj.age;
},
initial:{msum:0}
}
)

db.User.group({
key:{‘sex‘:true},
cond:{age:{$gt:23}},
reduce:function(obj,prev){
prev.agesum += obj.age;
},
initial:{agesum:0}
})
15:db.User.ensureIndex({name:1})
db.User.ensureIndex({name:1},{unique:true})

16:One to Many
db.News.save({ title:‘油价上涨,伤不起啊‘, author:‘zhangsan‘, date:‘2012-3-21‘, uid:[1,2,3,4], tag:["油价","中国","伤不起"]});

db.News.save({ author:"发改委", body:"发改委于3月20日上调油价,国内汽柴油价格每吨上调600元!", comments:[{ author: "犀利哥", body:"幸亏哥没车,涨吧,涨吧,都来像我这样吧!", date: "2012-3-20" },{ author: "小崔", body: "尼玛,坑爹啊!刚说话十块钱能买啥?你就上涨油价了~", date: "2012-3-20" }], date: "2012-3-20", tags: ["汽油", "油价", "发改委"], title: "发改委于3月20日上调油价,国内汽柴油价格每吨上调600元!" });

 

mongodb操作记录