/
updateMany.txt
151 lines (134 loc) · 3.28 KB
/
updateMany.txt
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# MongoDB db.collection.updateMany() method demo
# Insert some documents
bigboxcode> db.customer.insertMany([
{
"name": "Izabella Conroy",
"age": 19,
"phone": "108-679-2154",
"address": {
"street": "11 North Vineyard Drive",
"city": "Minneapolis",
"state": "Minnesota",
"postalCode": "19426",
"country": "US"
},
"profileCompletenessScore": 30
},
{
"name": "Lambert Purdy",
"age": 28,
"phone": "(610) 489-3633",
"address": {
"street": "305 2nd Ave",
"city": "Collegeville",
"state": "Minnesota",
"postalCode": "81007",
"country": "US"
},
"profileCompletenessScore": 40
},
{
"name": "Alisa Parker",
"age": 72,
"phone": "768-319-1054",
"address": {
"street": "8532 Ingalls Circle",
"city": "Arvada",
"state": "CO",
"postalCode": "80003",
"country": "CA"
},
"profileCompletenessScore": 60
},
{
"name": "Eloise Weber",
"age": 29,
"phone": "618-357-2104",
"address": {
"street": "632 Belmar Drive",
"city": "Edmond",
"state": "OK",
"postalCode": "73025",
"country": "CA"
},
"profileCompletenessScore": 80
}
])
output>
{
acknowledged: true,
insertedIds: {
'0': ObjectId('65d9ab2b339803ed78702c18'),
'1': ObjectId('65d9ab2b339803ed78702c19'),
'2': ObjectId('65d9ab2b339803ed78702c1a'),
'3': ObjectId('65d9ab2b339803ed78702c1b')
}
}
# Update based on country and state
# Update phone number and set latitude, longitude
bigboxcode> db.customer.updateMany(
{"address.country": "US", "address.state": "Minnesota"},
{$set: {"phone": "(310) 794-7217", "latitude": 34.061106, "longitude": -118.447428}}
)
output>
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
# Check if age and location exists
# Increase profileCompletenessScore
bigboxcode> db.customer.updateMany(
{age: {$exists: true}, latitude: {$exists: true}},
{$inc: {profileCompletenessScore: 20}}
)
output>
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
# Check by name
# Try to set state, and increase profileCompletenessScore
# Nothing updated as the named does not exist
bigboxcode> db.customer.updateMany(
{name: "non existing name"},
{$set: {"address.state": "NY"}, $inc: {profileCompletenessScore: 20}}
)
output>
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}
# Check by name
# set upsert=true
# document created as the named does not exist
bigboxcode> db.customer.updateMany(
{name: "non existing name"},
{$set: {"address.state": "NY"}, $inc: {profileCompletenessScore: 20}},
{upsert: true}
)
output>
{
acknowledged: true,
insertedId: ObjectId('65d9b38f3fe0144bdabd2bb2'),
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 1
}
# Check the recently created(upserted) data
bigboxcode> db.customer.findOne(ObjectId('65d9b38f3fe0144bdabd2bb2'))
output>
{
_id: ObjectId('65d9b38f3fe0144bdabd2bb2'),
name: 'non existing name',
address: { state: 'NY' },
profileCompletenessScore: 20
}