首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

mongodb学习小结

2013-03-27 
mongodb学习总结java操作mongodb实现CRUDhttp://my.oschina.net/chen106106/blog/49068http://blog.163.co

mongodb学习总结

java操作mongodb实现CRUD
http://my.oschina.net/chen106106/blog/49068
http://blog.163.com/wm_at163/blog/static/132173490201110254257510/
http://blog.csdn.net/itbasketplayer/article/details/8086061

spring集成mongodb时一定要统一spring-data-mongodb和spring-data-commons两个jar包的版本,并且spring的版本也要符合才行。

?

1.变换数据库
use [databasename]

2.insert创建数据库内容
post = {"title" : "My Blog Post",
"content":"Here's my blog post.",
"date":new Date()}

post = {"title" : "My Blog Post1",
"content":"Here's my blog post1.",
"date":new Date()}

将文档插入到集合中:
db.blog.insert(post)

3.查看集合中的文档
db.blog.find()
查看一个文档
db.blog.findOne()

4.更新文档
第一步修改变量post,增加“comments”键:
post.comments = []
db.blog.update({title:"My Blog Post"},post)

5.删除
db.blog.remove({title:"My Blog Post"})

6.获得集合名
db.getCollection("blog")

7遍历集合
var collections = {"posts","comments","authors"};

for(i in collections){
??? doStuff(db.blog[collections[i]]);
}

for i in range(1000000):
??? collection.insert({"foo":"bar","baz":i,"z":10-i});

8文档替换的例子
原文档
{
?"name":"joe",
?"friends":32,
?"enemies":2
}
修改后的文档
{
?"username":"joe",
?"relationships":
??{
???"friends":32,
???"enemies":2
??}
}

用update替换文档:
?var joe = db.users.findOne({"name":"joe"});
?joe.relationships = {"friends":joe.friends,"enemies":joe.enemies};
?
?joe.username = joe.name;
?delete joe.friends;
?delete joe.enemies;
?delete joe.name;

?db.users.update({"name":"joe"},joe)

9使用修改器
网站计数器:
{
?"utl":"www.baidu.com",
?"pageviews":52
}

db.analytics.update({"url":"www.baidu.com"},{"$inc":{"pageviews":1}})

? (1)"$set"修改器入门
?{
??"name":"joe",
??"age":"30",
??"sex":"male"
??"location":"Wisconsin"
?}

用户可以增加喜欢的书
? db.users.update({"name":"joe"},{"$set":{"favorite book":"war and peace"}})

用户喜欢另外的书籍
? db.users.update({"name":"joe"},{"$set":{"favorite book":"green eggs and ham"}})

用户喜欢一堆书
db.users.update({"rname":"joe"},{"$set":{"favorite book":["green eggs and ham","war and peace","cat's cradle"]}})

用户不喜欢书了
db.users.update({"name":"joe"}),{"$unset":{"favorite book":1}}

$set修改内嵌文档:
{
?"title":"a blog post",
?...
?"author":{
??"name":"joe",
??"email":"joe163.com"
??}
}
?
db.blog.posts.update({"author.name":"joe"},{"$set":{"author.name":"joe schmoe"}})

增加或者减少修改器:$inc
db.games.insert({"game":"pinball","user":"joe"})
db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":50}})
db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":10000}})

数组修改器:
db.blog.posts.findOne()
{
?"title":"",
?"content":"..."
}

添加评论为数组成员:
db.blog.posts.update({"title":"My Blog Post"},{"$push":{"comments":{"name":"jim","email":"jim163.com","content":"nice"}}})
再添加一条评论:
db.blog.posts.update({"title":"My Blog Post"},{"$push":{"comments":{"name":"Tom","email":"Tom163.com","content":"good post"}}})

$addToSet修改器添加到数组中避免重复
db.users.update({"name":"joe"},{"$addToSet":{"emails":"joe@163.com"}})
db.users.update({"name":"joe"},{"$addToSet":{"emails":"joe@mail.com"}})

$addToSet和$each组合实现添加多个数组成员
db.users.update({"name":"joe"},{"$addToSet":{"emails",{"$each":["joe@163.com","joe@126.com","joe@mail.com","joe@hot.com"]}}})

删除数组元素修改器:$pop
可以从数组任何一端删除元素
从数组末尾删除一个元素{$pop:{key:1}},从头部删除{$pop:{key:-1}}

删除特定数组中特定的数组:$pull

数组的定位修改器:定位符号("$")
db.blog.update({"name":"mary"},{"$inc":{"comments.0.votes":1}})

查询修改
db.blog.update({"name":"mary","comments.name":"Jim"},{"$set":{"comments.$.name":"Tom"}})

10修改器速度

11upsert
要是没有要更新的文档,就创建文档

12save Shell帮助程序
var x = db.foo.findOne()
x.num = 42

db.foo.save(x)

13更新多个文档
要使符合所有的文档进行更新,就使得第四个参数设为true
例:给所有特定日期过生日的用户一份礼物
db.users.update({"birthdaty":"10/13/1978"},
??? {"$set":{"gift":"Happy Birthday"}},false,true)

14查询
? 制定返回的键:
? db.users.find({},{"name":1,"_id":0})

? 查询条件:
? $lt??? $lte??? $gt?? $gte?? $ne
? <????? <=????? >???? >=???? !=

db.users.find({"age":{"$gte":18,"$lte":30}})

db.users.find({"name":{"$ne":"joe"}})

? $in查询以及 $nin
? db.raffle.find({"ticket_no":{"$in":[725,542,390]}})

? $or查询:
? db.raffle.find({"$or":[{"ticket_no":{"$in":[725,542,390]}},{"winner":true}]})

? $not和$mod
? db.users.find({"id_num":{"$mod":[5,1]}})//将查到的值除以给定的值(5)余??? 数是(1)就返回id_num,返回1,6,11,16...
? $not与$mod相反
? db.users.find({"id_num":{"$not":{"$mod":[5,1]}}});

? 正则表达式:
? db.users.find({"name":/joe/i});//不区分大小写查找joe
? db.users.find({"name":/joey?/i});//

? 查询数组:$all查询器
? db.food.find({"fruit":{"$all":["apple","banana"]}})//查询既有apple也有??? banana的文档
?
? 数组下标查询:db.food.find({"fruit.2":"peach"});

? $slice返回特定的数组个数
? db.food.find({"size":{"$gt":4}},{"fruit":{"$slice":3}})
? db.food.find({"_id":1},{"fruit":{"$slice":[2,3]}}) //2是位置,3是偏移量

? 查询内嵌文档:
? 运用“.”表示法来查询内嵌文档:db.people.find? ({"name.first":"Joe","name.last":"Schmoe"})

? db.blog.find({"comments":{"$elemMatch":{"author":"Joe","score":? {"$gte":5}}}}) //内嵌数组查询


? (注明:只有在走投无路时才考虑用where查询,因为他的效率太低。)
? $where强大的查询
? db.foo.insert({"apple":1,"banana":6,"peach":3})
? db.foo.insert({"apple":8,"spinach":4,"watermelon":4})
? 返回其中连个值相等的文档:
? db.foo.find({"$where":function(){
?? for(var current in this){
??? for(var other in this){
???? if(current != other && this[current] == this? [other]){
?return true;
? }
}
}
? return false;
}});

?

第五章? 索引
1.当查询中仅适用一个键时,可以对改建建立索引,以提高查询速度。
如:db.people.find({"username":"mark"});
建立索引使用ensureIndex方法:db.people.ensureIndex({"username":1})

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

热点排行