配置服务所需命令:
mongod --dbpath "\"d:\mongodb\data\db\"" --logpath "\"d:\mongodb\data\log\"" --install
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
要是没有要