mongo DB 第一天

最近没事看来一下mongoDB,貌似很牛逼的样子.因为他不像通常的关系型数据库那么多的约束.而且语法看上去和js一样.

mongod --dbpath C:\zhuyang\zhuyang\mongodb\mongodb\db

---insert
db.person.insert({"name":"zhuyang","sex":"male")
db.person.insert({"name":"hello","sex":"world"})

person = {"name":"nametest","sex":"sextest"}
db.person.insert(person)
---find
db.person.find()
db.person.find({"name":"nametest"})
db.person.findOne()
--update
db.person.update({"name":"nametest"},{"sex":"testupdate"})

--remove
db.person.remove({"name":"nametest"})
db.person.remove({"sex":"testupdate"})


--------------some command
help  ----see help doc
db.help();  --to see some command of db level
db.foo.help()  ----help of collections
to see the funcion implementation can use function name directly. eg: db.foo.update





---------insert to doc -------------
--insert one record 
db.foo.insert({"firstname":"zhu"}) --after this sentence , db will add another _id key(if doesnt defined) and save to doc.see below 
db.foo.insert({"lastname":"yang"})
db.foo.insert({"firstname":"chen"})
> db.foo.find()
{ "_id" : ObjectId("52ce585fe2e2fd0a66329ee3"), "firstname" : "zhu" }

------------batch insert
use mongoimport
file<4MB, need a field name _id

-----------remove doc
 db.foo.remove()  ---remove all 
 db.foo.remove({"firstname":"chen"}) ---remove one record

------update doc ------
see below 
db.user.insert(
	{
		"firstname":"zhu",
		"lastname":"yang",
		"friends":32,
		"enemies":2
	}
)

  

CHANGE TO 

{
		"username":"zhuyang",
		"relation":
		{
			"friends":32,
			"enemies":2
		}
	}
	
var zhuyang = db.user.findOne({"firstname":"zhu"})
zhuyang.relation={
			"friends":zhuyang.friends,
			"enemies":zhuyang.enemies 
}
zhuyang.username="zhuyang"
delete zhuyang.firstname
delete zhuyang.lastname
delete zhuyang.friends
delete zhuyang.enemies
db.user.update({"firstname":"zhu"},zhuyang)



 $set 修改器入门
 
db.user.insert(
	{
		"name":"joe",
		"age":30,
		"sex":"male",
		"location":"shanghai"
	}
)

//想要添加一个新的字段favorite book
//可用下面 2种办法
//1. 用update()
var joe = db.user.findOne({"name":"joe"})
joe.favoritebook="java";
db.user.update({"name":"joe"},joe);

//2. 使用修改器$set
db.user.update(
	{
		"name":"joe"
	},
	{
		"$set":{
		"favoritebook":"c++"
	}
	}
)

//也可以将favorite book改成一个数组

db.user.update(
	{
		"name":"joe"
	},
	{
		"$set":{
			"favoritebook":[
				"c++","c##","perl"
			]
	}
	}
)


//使用$unset 去移除
db.user.update(
	{
		"name":"joe"
	},
	{
		"$unset":{
			"favoritebook":1
		}
	}
)
//使用$set 修改内嵌文档
db.blog.posts.insert({
	"title":"java test",
	"content":"this is content",
	"author":{
		"name":"sb",
		"mail":"[email protected]"
	}
}
)

//修改作者名字为joe zhu
db.blog.posts.update(
	{
		"author.name":"sb"
	},
	{
		"$set":
		{
			"author.name":"joe zhu"
		}
	}
)

//$inc 修改器用来增加已有键的值,或者在键不存在的时候创建
db.games.insert(
	{
		"game":"pinball",
		"user":"joe"
	}
)
//如果小球碰到了砖块,就会给玩家加分.分数随便给,这里把玩家的基数分数设做50.使用$inc  给玩家加50


db.games.update(
	{
		"game":"pinball",
		"user":"joe"
	},
	{
		"$inc":{
			"score":50
		}
	}
)
//之前分数键是不存在的,所以用$inc创建了一个score键,并加量50
//如果小球落到指定位置,那么则加上1000分.

db.games.update(
	{
		"game":"pinball",
		"user":"joe"
	},
	{
		"$inc":{
			"score":1000
		}
	}
)
//现在的分数变成了50+1000=1050了 


//数组修改器
//如果指定的键已经存在,那么$push就会向已有的数组末尾加上一个元素,否则就会创建一个新的数组.

db.blog.posts.findOne({"author.mail":"[email protected]"});

{
        "_id" : ObjectId("52cf9f70cae129f46ff6fa5d"),
        "author" : {
                "mail" : "[email protected]",
                "name" : "joe zhu"
        },
        "content" : "this is content",
        "title" : "java test"
}

//比如现在要添加一个数组comments键.并发布一条评论:
db.blog.posts.update(
	{
		"author.mail":"[email protected]"
	},
	{
		"$push":{
			comments:{
			"name":"zhu",
			"email":"[email protected]",
			"content":"nice one"
			}
		}
	}
);

> db.blog.posts.find()
{ "_id" : ObjectId("52cf9f70cae129f46ff6fa5d"), "author" : { "mail" : "[email protected]", "name" : "joe zhu" }, "comments" : [   {       "name" : "zhu",         "email" : "[email protected]",
"content" : "nice one" } ], "content" : "this is content", "title" : "java test" }

//如果还要添加一条评论
db.blog.posts.update(
	{
	"author.mail":"[email protected]"
	},
	{
		"$push":{
			"comments":{
				"name":"yang",
				"email":"[email protected]",
				"content":"nice two"
			}
		}
	}
)


db.blog.posts.findOne()

      "_id" : ObjectId("52cf9f70cae129f46ff6fa5d"),
      "author" : {
              "mail" : "[email protected]",
              "name" : "joe zhu"
      },
      "comments" : [
              {
                      "name" : "zhu",
                      "email" : "[email protected]",
                      "content" : "nice one"
              },
              {
                      "name" : "yang",
                      "email" : "[email protected]",
                      "content" : "nice two"
              }
      ],
      "content" : "this is content",
      "title" : "java test"


使用$addToSet添加新的数组元素
db.blog.posts.update(
	{
	"author.mail":"[email protected]"
	},
	{
		"$addToSet":{
				"comments":{
				"name":"yang1",
				"email":"[email protected]",
				"content":"nice 3"
			}
		}
	}
)


使用$addToSet和$each添加多个元素

db.user.update(
	{
		"_id":ObjectId("52cf9c6dcae129f46ff6fa5c")
	},
	{
		"$addToSet":{
			"favoritebook":{
				"$each":[
					"javascript",
					"mongo in action"
				]
			}
		}
	}
)

//有几个从数组中删除元素的方法,如果把数组看作是一个栈或者队列.可以用$pop ,这个修改器可以从数组任何一端进行删除{$pop:{key:1}}从数组末端删除1个元素,{$pop:{key:-1}}从数组头删除1个元素
db.lists.insert(
	{
			"todo":[
				"dishes",
				"laundry",
				"dry cleaning"
			]
		}
)
db.lists.update(
		{
				"_id":ObjectId("52cfac11cae129f46ff6fa5f")
		},
		{
				"$pop":{
						"todo":-1
				}
		}
)
//有时基于特定条件来进行删除,而不是依据位置,"$pull"可以做到

db.lists.update(
		{
				"_id":ObjectId("52cfad13cae129f46ff6fa60")
		},
		{
				"$pull":{
						"todo":"laundry"
				}
		}
)

 //数组的定位修改器
 //若是数数组有多个值,而我们只想对其中一部分做修改.通过位置或者定位操作符$
 //数组都是从0开始的,我们可以通过下表进行访问元素
 > db.blog.posts.findOne()
{
        "_id" : ObjectId("52cfae34cae129f46ff6fa61"),
        "author" : {
                "name" : "sb",
                "mail" : "[email protected]"
        },
        "comments" : [
                {
                        "name" : "yang",
                        "email" : "[email protected]",
                        "content" : "nice two",
                        "vote" : 5
                },
                {
                        "name" : "yang1",
                        "email" : "[email protected]",
                        "content" : "nice 1",
                        "vote" : 6
                }
        ],
        "content" : "this is content",
        "title" : "java test"
}

//如果想要增加其中一个元素的投票数
db.blog.posts.update(
	{
		"_id":	ObjectId("52cfae34cae129f46ff6fa61")
	},
	{
		"$inc":{//这里的$inc用来做增加,也就是说vote数量将会在原有基础上增加100
				"comments.0.vote":100//comments.0第1个元素
		}
	}
)

//但是在很多情况下我们并不知道每一个元素的下标,这样就不能通过位置来操作, 而是用$来操作
db.blog.posts.update(
	{
		"comments.name":	"yang" //若有多个匹配,则只会操作一个
	},
	{
		"$set":{//直接设值
				"comments.$.vote":300 
		}
	}
)
 
 
//upsert 是一种特殊的更新方式,如果没文档符合更新条件,那么就会以这个条件和更新数据创建一个新的文档
db.user.update(
	{
		"edu":"gaozhong"//文档中没有edu==gaozhong
	},
	{
		"$set":{
				"age":100
		}
	},
	true//true表名这是一个upsert方式的更新
)
 db.user.find()
 { "_id" : ObjectId("52cfb32dedd18a0edc7db7b0"), "age" : 100, "edu" : "gaozhong" }
 
 

 

猜你喜欢

转载自zzzzzz5530041.iteye.com/blog/2002450
今日推荐