一、基本概念
文档:MongoDB的核心概念。多个键及其关联的值有序地放置在一起便是文档。相当于关系数据库中的行(但比行复杂)
集合:可以被看做没有模式的表,多个文档组成集合
数据库:多个集合组成数据库
MongoDB shell:MongoDB自带一个JavaScript shell,可以从命令行与MongoDB实例交互。
_id和ObjectId生成方式:
二、插入文档
————————————————————————
D:\mongodb\bin>mongo.exe
MongoDB shell version: 2.2.2
connecting to: test
> db.ysq.insert({"bar":"baz"}) /*插入文档*/
————————————————————————MongoDB shell version: 2.2.2
connecting to: test
> db.ysq.insert({"bar":"baz"}) /*插入文档*/
insert操作会给文档增加一个“_id”键(如果原来没有的话),然后将其保存到MongoDB中。
批量插入
三、删除文档
————————————————————————
> db.ysq.find()
> db.ysq.insert({"bar":"baz1"})
> db.ysq.insert({"bar":"baz2"})
> db.ysq.insert({"bar":"baz3"})
> db.ysq.find()
{ "_id" : ObjectId("50ec3b197224c3c23f44e49a"),"bar" :"baz1" }
{ "_id" : ObjectId("50ec3b1c7224c3c23f44e49b"),"bar" :"baz2" }
{ "_id" : ObjectId("50ec3b1f7224c3c23f44e49c"),"bar" :"baz3" }
> db.ysq.remove({"bar":"baz2"}) /*删除ysq集合中bar=baz2的文档*/
> db.ysq.find()
{ "_id" : ObjectId("50ec3b197224c3c23f44e49a"),"bar" :"baz1" }
{ "_id" : ObjectId("50ec3b1f7224c3c23f44e49c"),"bar" :"baz3" }
> db.ysq.remove() /*删除ysq集合中所有的文档*/
> db.ysq.find()
>
————————————————————————>
remove操作不会删除集合本身,原有的索引也会保留,如果数据库较大,可先删除整个集合,再重建索引,这样执行会比较快
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50ec3daf7224c3c23f44e49d"),"bar" :"baz" }
> db.ysq.drop() /*删除集合ysq*/
true
> db.ysq.find()
>
————————————————————————{ "_id" : ObjectId("50ec3daf7224c3c23f44e49d"),"bar" :"baz" }
> db.ysq.drop() /*删除集合ysq*/
true
> db.ysq.find()
>
三、更新文档
文档存入数据库以后快就可以使用update方法来修改它。update有两个参数,一个是查询文档,用来找出要更新的文档,拎一个是修改器文档,描述对找到的文档做哪些更改。更新的操作是原子的。
3.1 文档替换
————————————————————————
> me = { /*原有文档结构*/
... "name":"ysq",
... "friends":32,
... "enemies":2
... }
{ "name" : "ysq","friends" :32,"enemies" :2 }
> db.ysq.insert(me);
> me1 = { /*更新后的新文档结构*/
... "username":"ysq",
... "relationships":{
... "friends":32,
... "enemies":2
... }
... }
{
"username" : "ysq",
"relationships" : {
"friends" : 32,
"enemies" : 2
}
}
> var me2 = db.users.findOne({"name":"ysq"});
> me2.relationships={"friends":me2.friends,"enemies":me2.enemies};
{ "friends" : 32,"enemies" :2 }
> me2.username = me2.name;
ysq
> delete me2.friends;
true
> delete me2.enemies;
true
> delete me2.name;
true
> db.ysq.update({"name":"ysq"},me2); /*该选择器如果匹配到多个文档,数据库将会报错*/
> db.ysq.findOne({"username":"ysq"})
{
"_id" : ObjectId("50ec3fd67224c3c23f44e49e"),
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "ysq"
}
————————————————————————... "name":"ysq",
... "friends":32,
... "enemies":2
... }
{ "name" : "ysq","friends" :32,"enemies" :2 }
> db.ysq.insert(me);
> me1 = { /*更新后的新文档结构*/
... "username":"ysq",
... "relationships":{
... "friends":32,
... "enemies":2
... }
... }
{
"username" : "ysq",
"relationships" : {
"friends" : 32,
"enemies" : 2
}
}
> var me2 = db.users.findOne({"name":"ysq"});
> me2.relationships={"friends":me2.friends,"enemies":me2.enemies};
{ "friends" : 32,"enemies" :2 }
> me2.username = me2.name;
ysq
> delete me2.friends;
true
> delete me2.enemies;
true
> delete me2.name;
true
> db.ysq.update({"name":"ysq"},me2); /*该选择器如果匹配到多个文档,数据库将会报错*/
> db.ysq.findOne({"username":"ysq"})
{
"_id" : ObjectId("50ec3fd67224c3c23f44e49e"),
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "ysq"
}
3.2使用修改器
1、$inc 修改器
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"url" :"www.baidu.com","pageviews" :1 }
> db.ysq.update({"url":"www.baidu.com"},{"$inc":{"pageviews":1}})
> db.ysq.find();
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"url" :"www.baidu.com","pageviews" :2 }
————————————————————————{ "_id" : ObjectId("50ee418460d1d99460573b83"),"url" :"www.baidu.com","pageviews" :1 }
> db.ysq.update({"url":"www.baidu.com"},{"$inc":{"pageviews":1}})
> db.ysq.find();
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"url" :"www.baidu.com","pageviews" :2 }
以上例子,如果pageviews不存在则创建,存在则增加1。
2、$set 修改器
a、修改值
————————————————————————
> db.ysq.update({"_id":ObjectId("50ee418460d1d99460573b83")},{"$set":{"url":"www.google.com"}})
> db.ysq.find()
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"pageviews" :2,"url" :"www.google.com" }
————————————————————————> db.ysq.find()
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"pageviews" :2,"url" :"www.google.com" }
b、修改数据类型
————————————————————————
> db.ysq.update({"_id":ObjectId("50ee418460d1d99460573b83")},{"$set":{"url":["www.baidu.com","google.com"]}})
————————————————————————c、修改文档内嵌文档
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50ee463260d1d99460573b84"),"student" : {"name" :"ysq","email" :"ysq@ysq.com" } }
> db.ysq.update({"_id" : ObjectId("50ee463260d1d99460573b84")},{"$set":{"student.email":"ysq@163.com"}})
> db.ysq.find()
{ "_id" : ObjectId("50ee463260d1d99460573b84"),"student" : {"name" :"ysq","email" :"ysq@163.com" } }
————————————————————————{ "_id" : ObjectId("50ee463260d1d99460573b84"),"student" : {"name" :"ysq","email" :"ysq@ysq.com" } }
> db.ysq.update({"_id" : ObjectId("50ee463260d1d99460573b84")},{"$set":{"student.email":"ysq@163.com"}})
> db.ysq.find()
{ "_id" : ObjectId("50ee463260d1d99460573b84"),"student" : {"name" :"ysq","email" :"ysq@163.com" } }
3、$unset 修改器
将键完全删除
————————————————————————
> db.ysq.update({"_id":ObjectId("50ee418460d1d99460573b83")},{"$unset":{"url":1}})
> db.ysq.find()
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"pageviews" :2 }
————————————————————————> db.ysq.find()
{ "_id" : ObjectId("50ee418460d1d99460573b83"),"pageviews" :2 }
4、数组修改器
指定用在值为数组的键上
a、$push
如果指定的键存在,将向已有的数组末尾加入一个元素,如果不存在则创建一个新的数组
————————————————————————
> db.ysq.update({ "_id" : ObjectId("50ee49b060d1d99460573b87")},{"$push":{"array":"a1"}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1" ],"name" :"arrayTest" }
> db.ysq.update({ "_id" : ObjectId("50ee49b060d1d99460573b87")}, {"$push":{"array":"a2"}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2" ],"name":"arrayTest" }
>
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1" ],"name" :"arrayTest" }
> db.ysq.update({ "_id" : ObjectId("50ee49b060d1d99460573b87")}, {"$push":{"array":"a2"}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2" ],"name":"arrayTest" }
>
————————————————————————
b、$addToSet
可以避免重复
————————————————————————
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{"$addToSet":{"ar
ray":"a1"}});
> db.ysq.find() /* a1已经有了,不创建 */
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2" ],"name":"arrayTest" }
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{"$addToSet":{"array":"a3"}});
> db.ysq.find() /* a2被创建 */
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2","a3" ],"name" :"arrayTest" }
>
ray":"a1"}});
> db.ysq.find() /* a1已经有了,不创建 */
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2" ],"name":"arrayTest" }
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{"$addToSet":{"array":"a3"}});
> db.ysq.find() /* a2被创建 */
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2","a3" ],"name" :"arrayTest" }
>
————————————————————————c、$each
————————————————————————
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{"$addToSet":{"array":{"$each":["a2","a3","a4"]}}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2","a3","a" ],"name" :"arrayTest" }
>
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2","a3","a" ],"name" :"arrayTest" }
>
————————————————————————d、$pop
{$pop : {key : 1 }} 从数组末尾删除一个元素
{$pop : {key : -1 }} 从数组头部删除一个元素
————————————————————————
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{"$pop":{"array":1}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2","a3" ],"name" :"arrayTest" }
>
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{$pop:{"array":-1}})/*可不带双引号*/
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a2","a3" ],"name" :"arrayTest" }
>
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a1","a2","a3" ],"name" :"arrayTest" }
>
> db.ysq.update({"_id" : ObjectId("50ee49b060d1d99460573b87")},{$pop:{"array":-1}})/*可不带双引号*/
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : ["a2","a3" ],"name" :"arrayTest" }
>
————————————————————————e、$pull
$pull将所有匹配的部分删掉
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : [1,1,2,1 ] }
> db.ysq.update({ "_id" : ObjectId("50ee49b060d1d99460573b87")},{"$pull":{"array":1}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : [2 ] }
>
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : [1,1,2,1 ] }
> db.ysq.update({ "_id" : ObjectId("50ee49b060d1d99460573b87")},{"$pull":{"array":1}})
> db.ysq.find()
{ "_id" : ObjectId("50ee49b060d1d99460573b87"),"array" : [2 ] }
>
————————————————————————f、数组的定位修改器
下标定位
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":1 }, { "name" :"b","int" :3 } ] }
> db.ysq.update({"_id" : ObjectId("50f41573a8d5afd1a7c5eb03")},{"$inc":{"com.0.int":1}})
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int": 2 }, {"name" :"b","int" :3 } ] }
>
————————————————————————{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":1 }, { "name" :"b","int" :3 } ] }
> db.ysq.update({"_id" : ObjectId("50f41573a8d5afd1a7c5eb03")},{"$inc":{"com.0.int":1}})
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int": 2 }, {"name" :"b","int" :3 } ] }
>
$定位:
只更新第一个匹配到的文档
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int": 2 }, {"name" :"b","int" :3 } ] }
> db.ysq.update({"com.name":"a"},{"$inc":{"com.$.int":1}})
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int": 3 }, {"name" :"b","int" :3 } ] }
>
————————————————————————{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int": 2 }, {"name" :"b","int" :3 } ] }
> db.ysq.update({"com.name":"a"},{"$inc":{"com.$.int":1}})
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int": 3 }, {"name" :"b","int" :3 } ] }
>
3.3 upsert
update的第3个参数表上这个是upsert
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":3 }, {"name" :"b","int" :3 } ] }
> db.ysq.update({"com.name":"a"},{"$inc":{"com.$.int":1}},true)/*存在文档,则更新*/
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":4 }, {"name" :"b","int" :3 } ] }
> db.ysq.update({"com.name":"c"},{"$inc":{"com.$.int":1}},true))/*不存在文档,则创建*/
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":4 }, {"name" :"b","int" :3 } ] }
{ "_id" : ObjectId("50f41991722df94eac301a08"),"com" : {"$" : {"int" :1 },"name" :"c" } }
>
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":3 }, {"name" :"b","int" :3 } ] }
> db.ysq.update({"com.name":"a"},{"$inc":{"com.$.int":1}},true)/*存在文档,则更新*/
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":4 }, {"name" :"b","int" :3 } ] }
> db.ysq.update({"com.name":"c"},{"$inc":{"com.$.int":1}},true))/*不存在文档,则创建*/
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ {"name" :"a","int":4 }, {"name" :"b","int" :3 } ] }
{ "_id" : ObjectId("50f41991722df94eac301a08"),"com" : {"$" : {"int" :1 },"name" :"c" } }
>
————————————————————————3.4 更新多个文档
默认情况下,更新只能对符合匹配条件的第一个文档执行操作。要使所有匹配的文档都得到更新,可以设置update 的第4个参数为true。
建议每次都显式表明要不要做多文档更新。
————————————————————————
> db.ysq.find()
{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ { "name" :"a", "int":6 }, { "name" :"b", "int" :4 } ] }
{ "_id" : ObjectId("50f41991722df94eac301a08"),"com" : { "$" : {"int" : 1 },"0" : { "int" :1 }, "name" :"c" } }
> db.runCommand({getLastError:1})
{ "n" : 0, "connectionId" : 2, "err" : null, "ok" : 1 }
> db.ysq.update({"com.0.name":{"$ne":"n"}},{"$inc":{"com.0.int":1}},false,true)
> db.runCommand({getLastError:1})
{
"updatedExisting" : true,
"n" : 2,/*表示多文档更新更新了2个文档*/
"connectionId" : 2,
"err" : null,
"ok" : 1
}
>
————————————————————————{ "_id" : ObjectId("50f41573a8d5afd1a7c5eb03"),"com" : [ { "name" :"a", "int":6 }, { "name" :"b", "int" :4 } ] }
{ "_id" : ObjectId("50f41991722df94eac301a08"),"com" : { "$" : {"int" : 1 },"0" : { "int" :1 }, "name" :"c" } }
> db.runCommand({getLastError:1})
{ "n" : 0, "connectionId" : 2, "err" : null, "ok" : 1 }
> db.ysq.update({"com.0.name":{"$ne":"n"}},{"$inc":{"com.0.int":1}},false,true)
> db.runCommand({getLastError:1})
{
"updatedExisting" : true,
"n" : 2,/*表示多文档更新更新了2个文档*/
"connectionId" : 2,
"err" : null,
"ok" : 1
}
>
------------------------- (完)-------------------------