這節來說說mongodb更新操作,可以使用update()函數來對資料進行更新。
文法:db.collection.update( criteria, objNew, upsert, multi )
update()接受的四個參數含義如下:
criteria : update的查詢條件哪些記錄需要更新,類似于SQL update語句的where子句。
objNew : update的對象和一些更新的操作符如$,$inc等等,也可以了解為SQL update語句的set子句。
upsert : 如果不存在update的記錄,是否插入objNew,true為插入,預設是false,不插入。
multi : 預設是false,隻更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。
注意:multi隻對$操作有效。
1. 執行個體1
> db.mediaCollection.find({"Title" : "Matrix, The"}).toArray()
[
{
"_id" : ObjectId("53548225d85b463e729a2e57"),
"Type" : "DVD",
"Title" : "Matrix, The",
"Released" : 1999,
"Cast" : [
"Keanu Reeves",
"Carry-Anne Moss",
"Laurence Fishburne",
"Hugo Weaving",
"Gloria Foster",
"Joe Pantoliano"
]
}
]
> db.mediaCollection.update({"Title" : "Matrix, The"},{"Type" : "DVD","Title" : "Matrix, The","Released" : 1999,"Genre":"Action"},true)
> db.mediaCollection.find({"Title" : "Matrix, The"}).toArray()
[
{
"_id" : ObjectId("53548225d85b463e729a2e57"),
"Type" : "DVD",
"Title" : "Matrix, The",
"Released" : 1999,
"Genre" : "Action"
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
>db.mediaCollection.find({"Title":"Matrix, The"}).toArray()
[
{
"_id":ObjectId("53548225d85b463e729a2e57"),
"Type":"DVD",
"Title":"Matrix, The",
"Released":1999,
"Cast":[
"Keanu Reeves",
"Carry-Anne Moss",
"Laurence Fishburne",
"Hugo Weaving",
"Gloria Foster",
"Joe Pantoliano"
]
}
]
>db.mediaCollection.update({"Title":"Matrix, The"},{"Type":"DVD","Title":"Matrix, The","Released":1999,"Genre":"Action"},true)
>db.mediaCollection.find({"Title":"Matrix, The"}).toArray()
[
{
"_id":ObjectId("53548225d85b463e729a2e57"),
"Type":"DVD",
"Title":"Matrix, The",
"Released":1999,
"Genre":"Action"
}
]
2. 使用save()指令來完成upset操作
文法:
db.collection.save(
,
{
writeConcern:
}
)
1
2
3
4
5
6
db.collection.save(
,
{
writeConcern:
}
)
> db.mediaCollection.save( {"Type" : "DVD", "Title" : "Matrix, the", "Released" : "1999", "Genre" : "Action"})
1
>db.mediaCollection.save({"Type":"DVD","Title":"Matrix, the","Released":"1999","Genre":"Action"})
3. 遞增值$inc
$inc允許給一個指定的鍵加上增量,如果該鍵不存在将建立。
> db.mediaCollection.insert({ "Type" : "Manga", "Title" : "One Piece", "Volumes" : 612,"Read" : 520 })
> db.mediaCollection.update({"Title" : "One Piece"},{$inc:{"Read":5}})
> db.mediaCollection.find({"Title" : "One Piece"}).toArray()
[
{
"_id" : ObjectId("53560b8be0e2e4dd9bb0683d"),
"Type" : "Manga",
"Title" : "One Piece",
"Volumes" : 612,
"Read" : 525
}
]
1
2
3
4
5
6
7
8
9
10
11
12
>db.mediaCollection.insert({"Type":"Manga","Title":"One Piece","Volumes":612,"Read":520})
>db.mediaCollection.update({"Title":"One Piece"},{$inc:{"Read":5}})
>db.mediaCollection.find({"Title":"One Piece"}).toArray()
[
{
"_id":ObjectId("53560b8be0e2e4dd9bb0683d"),
"Type":"Manga",
"Title":"One Piece",
"Volumes":612,
"Read":525
}
]
4. $set設定字段值
可以設定任何資料類型。
> db.mediaCollection.find({"Title" : "Matrix, the"}).toArray()
[
{
"_id" : ObjectId("535609c4e0e2e4dd9bb0683c"),
"Type" : "DVD",
"Title" : "Matrix, the",
"Released" : "1999",
"Genre" : "Action"
}
]
>
>
>
> db.mediaCollection.update({"Title" : "Matrix, the"}, {$set:{"Genre" : "Sci-Fi"}})
> db.mediaCollection.find({"Title" : "Matrix, the"}).toArray()
[
{
"_id" : ObjectId("535609c4e0e2e4dd9bb0683c"),
"Type" : "DVD",
"Title" : "Matrix, the",
"Released" : "1999",
"Genre" : "Sci-Fi"
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>db.mediaCollection.find({"Title":"Matrix, the"}).toArray()
[
{
"_id":ObjectId("535609c4e0e2e4dd9bb0683c"),
"Type":"DVD",
"Title":"Matrix, the",
"Released":"1999",
"Genre":"Action"
}
]
>
>
>
>db.mediaCollection.update({"Title":"Matrix, the"},{$set:{"Genre":"Sci-Fi"}})
>db.mediaCollection.find({"Title":"Matrix, the"}).toArray()
[
{
"_id":ObjectId("535609c4e0e2e4dd9bb0683c"),
"Type":"DVD",
"Title":"Matrix, the",
"Released":"1999",
"Genre":"Sci-Fi"
}
]
5. $unset删除指定字段
> db.mediaCollection.find({"Title" : "Matrix, the"}).toArray()
[
{
"_id" : ObjectId("535609c4e0e2e4dd9bb0683c"),
"Type" : "DVD",
"Title" : "Matrix, the",
"Released" : "1999",
"Genre" : "Sci-Fi"
}
]
> db.mediaCollection.update({"Title" : "Matrix, the"}, {$unset:{"Genre" : 1}})
> db.mediaCollection.find({"Title" : "Matrix, the"}).toArray()
[
{
"Released" : "1999",
"Title" : "Matrix, the",
"Type" : "DVD",
"_id" : ObjectId("535609c4e0e2e4dd9bb0683c")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>db.mediaCollection.find({"Title":"Matrix, the"}).toArray()
[
{
"_id":ObjectId("535609c4e0e2e4dd9bb0683c"),
"Type":"DVD",
"Title":"Matrix, the",
"Released":"1999",
"Genre":"Sci-Fi"
}
]
>db.mediaCollection.update({"Title":"Matrix, the"},{$unset:{"Genre":1}})
>db.mediaCollection.find({"Title":"Matrix, the"}).toArray()
[
{
"Released":"1999",
"Title":"Matrix, the",
"Type":"DVD",
"_id":ObjectId("535609c4e0e2e4dd9bb0683c")
}
]
6. $push追加一個值到指定字段
$push允許追加一個值到指定字段。如果該字段是一個現有的數組,那麼該值将被添加。如果該字段尚不存在,則該字段将被設定為該值的數組。如果該字段存在,但它不是一個數組,将報錯。
$push針對數組類型的。
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"_id" : ObjectId("5353462f93efef02c962da71"),
"Type" : "Book",
"Title" : "Definitive Guide to MongoDB, the",
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim"
]
}
]
>
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$push:{Author:"Griffin, Stewie"}})
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$push:{Title:"Griffin, Stewie"}})
Cannot apply $push/$pushAll modifier to non-array
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"_id":ObjectId("5353462f93efef02c962da71"),
"Type":"Book",
"Title":"Definitive Guide to MongoDB, the",
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim"
]
}
]
>
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$push:{Author:"Griffin, Stewie"}})
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$push:{Title:"Griffin, Stewie"}})
Cannotapply$push/$pushAllmodifiertonon-array
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
7. 向一個數組添加多個值
$pushAll與$push類似,規則相同。向指定的數組添加多個值。
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$pushAll:{Author: ["Griffin,Louis","Griffin, Peter"]}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$pushAll:{Author:["Griffin,Louis","Griffin, Peter"]}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
8. $addToSet将資料添加到數組
向數組添加資料的另一種方式。然而,該操作隻有該資料不存在時添加。同時,隻能指定一個參數,如果要指定多個參數可以使用$each.
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
>
>
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$addToSet:{Author:"Griffin, Stewie"}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$addToSet:{Author:{$each:["Griffin, Stewie","Griffin, Meg"]}}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter",
"Griffin, Meg"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>
>
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$addToSet:{Author:"Griffin, Stewie"}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$addToSet:{Author:{$each:["Griffin, Stewie","Griffin, Meg"]}}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter",
"Griffin, Meg"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
9. 從數組中删除元素
有多種方式從數組中删除元素: $pop, $pull, $pullAll。
$pop可以删除數組第一個或最後一個元素。1最後一個元素,-1第一個元素。
$pull從數組中删除指定的元素。
$pullAll從數組中删除多個不同的元素。
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter",
"Griffin, Meg"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$pop:{Author:1}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter",
"Griffin, Meg"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$pop:{Author:1}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$pop:{Author:-1}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$pop:{Author:-1}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter",
"Plugge, Eelco",
"Griffin, Stewie"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$pull:{Author:"Griffin, Stewie"}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Griffin,Louis",
"Griffin, Peter",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin,Louis",
"Griffin, Peter",
"Plugge, Eelco",
"Griffin, Stewie"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$pull:{Author:"Griffin, Stewie"}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Griffin,Louis",
"Griffin, Peter",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Griffin,Louis",
"Griffin, Peter",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.update({"ISBN" : "987-1-4302-3051-9"},{$pullAll:{Author:["Griffin,Louis", "Griffin, Peter",]}})
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Griffin,Louis",
"Griffin, Peter",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.update({"ISBN":"987-1-4302-3051-9"},{$pullAll:{Author:["Griffin,Louis","Griffin, Peter",]}})
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
10. 指定位置來比對數組
可以使用$操作符在查詢中指定的查詢比對的數組項的位置。
> db.mediaCollection.update( { "Title" : "Nevermind" }, {$addToSet : { Tracklist :{"Track" : 2,"Title": "Been a son", "Length":"2:23"} } } )
> db.mediaCollection.find({"Title" : "Nevermind"}).toArray()
[
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
"Title" : "Smells like teen spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
},
{
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : 2,
"Title" : "Been a son",
"Length" : "2:23"
}
],
"Type" : "CD",
"_id" : ObjectId("5353462f93efef02c962da72")
}
]
> db.mediaCollection.update( { "Tracklist.Title" : "Been a son"},{$inc:{"Tracklist.$.Track" : 1} } )
> db.mediaCollection.find({"Title" : "Nevermind"}).toArray()
[
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
"Title" : "Smells like teen spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
},
{
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : 3,
"Title" : "Been a son",
"Length" : "2:23"
}
],
"Type" : "CD",
"_id" : ObjectId("5353462f93efef02c962da72")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
>db.mediaCollection.update({"Title":"Nevermind"},{$addToSet:{Tracklist:{"Track":2,"Title":"Been a son","Length":"2:23"}}})
>db.mediaCollection.find({"Title":"Nevermind"}).toArray()
[
{
"_id":ObjectId("5353463193efef02c962da73"),
"Type":"CD",
"Artist":"Nirvana",
"Title":"Nevermind",
"Tracklist":[
{
"Track":"1",
"Title":"Smells like teen spirit",
"Length":"5:02"
},
{
"Track":"2",
"Title":"In Bloom",
"Length":"4:15"
}
]
},
{
"Artist":"Nirvana",
"Title":"Nevermind",
"Tracklist":[
{
"Track":2,
"Title":"Been a son",
"Length":"2:23"
}
],
"Type":"CD",
"_id":ObjectId("5353462f93efef02c962da72")
}
]
>db.mediaCollection.update({"Tracklist.Title":"Been a son"},{$inc:{"Tracklist.$.Track":1}})
>db.mediaCollection.find({"Title":"Nevermind"}).toArray()
[
{
"_id":ObjectId("5353463193efef02c962da73"),
"Type":"CD",
"Artist":"Nirvana",
"Title":"Nevermind",
"Tracklist":[
{
"Track":"1",
"Title":"Smells like teen spirit",
"Length":"5:02"
},
{
"Track":"2",
"Title":"In Bloom",
"Length":"4:15"
}
]
},
{
"Artist":"Nirvana",
"Title":"Nevermind",
"Tracklist":[
{
"Track":3,
"Title":"Been a son",
"Length":"2:23"
}
],
"Type":"CD",
"_id":ObjectId("5353462f93efef02c962da72")
}
]
11. 原子操作
要麼成功要麼復原。
12. 擷取最近一次更改資訊
> db.$cmd.findOne({getlasterror:1})
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
> db.mediaCollection.update( { "Tracklist.Title" : "In Bloom"},{$inc:{"Tracklist.$.Track" : 1} } )
Cannot apply $inc modifier to non-number
> db.$cmd.findOne({getlasterror:1})
{
"err" : "Cannot apply $inc modifier to non-number",
"code" : 10140,
"n" : 0,
"connectionId" : 1,
"ok" : 1
}
1
2
3
4
5
6
7
8
9
10
11
12
>db.$cmd.findOne({getlasterror:1})
{"n":0,"connectionId":1,"err":null,"ok":1}
>db.mediaCollection.update({"Tracklist.Title":"In Bloom"},{$inc:{"Tracklist.$.Track":1}})
Cannotapply$incmodifiertonon-number
>db.$cmd.findOne({getlasterror:1})
{
"err":"Cannot apply $inc modifier to non-number",
"code":10140,
"n":0,
"connectionId":1,
"ok":1
}
13. ABA問題
ABA Problem就是譬如一個共享變量A,需要update到一個新的值C,而記錄這個變量的值是B,此時A=B;在記錄和update的這段時間中,這個共享變量被其餘的job或者是thread操作修改了很多次,但是在update之前,還是回到了最開始的值。這就叫做ABA Problem。
避免方法:
1). 使用整體對象查詢表達式代替_id和comments.by。
2). 使用$set來更新。
3). 版本變量控制。
4). 如果可能,使用$來代替update-if-current操作序列。
14. findAndModify()
文法:
db.collection.findAndModify({
query: ,
sort: ,
remove: ,
update: ,
new: ,
fields: ,
upsert:
});
1
2
3
4
5
6
7
8
9
db.collection.findAndModify({
query:,
sort:,
remove:,
update:,
new:,
fields:,
upsert:
});
原子更新,該指令查詢并修改文檔。
> db.mediaCollection.findAndModify( { "Title" : "One Piece",sort:{"Title": -1}, remove:true} )
{
"_id" : ObjectId("53560b8be0e2e4dd9bb0683d"),
"Type" : "Manga",
"Title" : "One Piece",
"Volumes" : 612,
"Read" : 525
}
> db.mediaCollection.find({"Title" : "One Piece"}).toArray()
[ ]
1
2
3
4
5
6
7
8
9
10
>db.mediaCollection.findAndModify({"Title":"One Piece",sort:{"Title":-1},remove:true})
{
"_id":ObjectId("53560b8be0e2e4dd9bb0683d"),
"Type":"Manga",
"Title":"One Piece",
"Volumes":612,
"Read":525
}
>db.mediaCollection.find({"Title":"One Piece"}).toArray()
[]
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.findAndModify( { query: { "ISBN" : "987-1-4302-3051-9" }, sort:{"Title":-1}, update: {$set: {"Title" : " Different Title"} } } )
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, the",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : " Different Title",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.findAndModify({query:{"ISBN":"987-1-4302-3051-9"},sort:{"Title":-1},update:{$set:{"Title":" Different Title"}}})
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":"Definitive Guide to MongoDB, the",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":" Different Title",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
上面執行個體更新“Title”并傳回舊文檔。要傳回更改後的文檔按下面操作:
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : " Different Title",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
> db.mediaCollection.findAndModify( { query: { "ISBN" : "987-1-4302-3051-9" }, sort:{"Title":-1}, update: {$set: {"Title" : " Different Title 2"} },new:true } )
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : " Different Title 2",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
> db.mediaCollection.find({"ISBN" : "987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author" : [
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Title" : " Different Title 2",
"Type" : "Book",
"_id" : ObjectId("5353462f93efef02c962da71")
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":" Different Title",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
]
>db.mediaCollection.findAndModify({query:{"ISBN":"987-1-4302-3051-9"},sort:{"Title":-1},update:{$set:{"Title":" Different Title 2"}},new:true})
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":" Different Title 2",
"Type":"Book",
"_id":ObjectId("5353462f93efef02c962da71")
}
>db.mediaCollection.find({"ISBN":"987-1-4302-3051-9"}).sort({"Title":-1}).toArray()
[
{
"Author":[
"Hawkins, Tim",
"Plugge, Eelco"
],
"ISBN":"987-1-4302-3051-9",
"Publisher":"Apress",
"Title":" Different Title 2",
"Type":"Book",