MongoDB推送到嵌套数组或插入新文档

时间:2019-12-02 21:00:17

标签: python mongodb mongodb-query pymongo upsert

我正在将Python的库用于Mongo(pymongo),我的文档如下所示:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="600.000000pt" height="800.000000pt" viewBox="0 0 600.000000 800.000000"
 preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,800.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M395 7131 c3 -5 20 -12 38 -16 17 -4 124 -32 237 -62 113 -30 252
-67 310 -83 207 -55 455 -124 645 -180 61 -18 173 -49 250 -70 77 -21 273 -75
435 -120 162 -45 394 -108 515 -140 121 -31 252 -66 290 -77 103 -30 284 -79
415 -112 63 -16 140 -36 170 -44 204 -58 788 -213 1235 -327 83 -21 194 -50
249 -64 54 -14 108 -26 119 -26 37 0 5 17 -63 34 -36 9 -123 32 -195 51 -71
20 -159 42 -195 50 -36 8 -90 21 -120 29 -30 8 -107 28 -170 45 -127 33 -711
190 -830 223 -174 47 -296 78 -313 78 -10 0 -15 4 -12 10 3 5 0 7 -8 5 -7 -3
-78 13 -158 35 -79 22 -211 58 -294 79 -82 22 -242 65 -355 96 -113 31 -263
72 -335 91 -71 20 -150 42 -175 49 -25 8 -130 37 -235 65 -104 28 -260 71
-345 96 -234 68 -313 90 -455 128 -71 19 -241 64 -376 100 -246 66 -285 74
-274 57z"/>
<path d="M540 7031 c0 -6 4 -13 10 -16 6 -3 7 1 4 9 -7 18 -14 21 -14 7z"/>
<path d="M180 6903 c0 -17 22 -38 26 -26 5 11 48 0 534 -128 85 -23 183 -48
216 -56 l61 -16 7 -96 c7 -94 10 -158 21 -391 12 -274 18 -366 22 -373 3 -5
-34 -5 -81 -1 -73 6 -86 5 -86 -8 0 -12 12 -16 48 -16 26 0 98 -5 160 -12 63
-6 116 -9 119 -7 15 16 -15 27 -71 27 l-64 0 -6 103 c-3 56 -8 158 -11 227 -3
69 -8 143 -10 165 -3 22 -7 85 -10 140 -3 55 -8 131 -11 169 -6 63 -5 68 12
62 20 -6 514 -144 629 -176 84 -23 312 -86 410 -112 39 -10 106 -29 150 -42
92 -28 303 -85 355 -96 19 -4 71 -17 115 -30 44 -12 127 -35 185 -50 58 -15
173 -46 255 -69 83 -23 191 -51 240 -62 50 -11 121 -29 158 -40 70 -21 316
-85 377 -99 19 -4 58 -15 85 -23 65 -20 531 -140 900 -231 l290 -72 -1 -120
c-1 -65 -2 -714 -3 -1441 -1 -1038 -4 -1323 -14 -1326 -9 -4 -9 -7 0 -14 10
-7 13 -85 15 -308 2 -194 7 -299 13 -298 6 2 11 106 12 280 2 153 3 291 3 306
l0 29 74 -3 c41 -2 78 -1 81 3 9 9 -67 20 -112 17 -40 -3 -43 -1 -43 31 0 18
7 20 77 20 57 0 74 3 66 11 -7 7 -36 10 -76 7 l-66 -4 -3 71 c-2 38 -5 271 -6
518 l-1 447 84 0 c54 0 85 4 85 11 0 7 -28 9 -82 7 -80 -3 -83 -2 -86 20 -3
21 -1 22 92 22 57 0 96 4 96 10 0 6 -38 10 -95 10 l-95 0 0 531 0 531 63 -6
c34 -4 79 -9 100 -12 22 -4 37 -2 37 5 0 11 -27 17 -130 29 -48 5 -65 11 -65
22 0 12 16 15 85 16 47 0 96 1 110 2 22 2 23 4 7 13 -9 5 -22 7 -27 4 -5 -3
-47 -3 -92 1 l-83 6 3 256 c2 184 7 259 15 264 9 6 9 8 0 8 -7 0 -13 4 -13 9
0 4 12 6 28 3 24 -4 25 -3 12 11 -8 8 -82 31 -165 51 -82 20 -175 43 -205 51
-30 8 -77 19 -105 24 -27 5 -102 23 -165 41 -63 17 -227 61 -365 97 -267 69
-325 84 -505 133 -63 16 -245 64 -405 105 -159 41 -333 87 -385 101 -52 14
-255 68 -450 120 -195 53 -409 111 -475 131 -66 19 -131 37 -145 40 -21 5
-242 65 -575 158 -44 12 -126 34 -182 50 -81 22 -103 32 -105 47 -2 10 -7 18
-13 16 -5 -1 -9 -8 -7 -15 4 -17 2 -17 -103 12 -103 29 -603 163 -655 176 -43
11 -80 11 -80 2z"/>
<path d="M950 4881 c-5 -11 -10 -22 -10 -25 0 -3 38 -6 84 -6 l83 0 7 -39 c6
-39 29 -565 41 -951 4 -113 8 -207 10 -210 2 -3 6 -93 9 -200 4 -107 8 -229
11 -270 3 -41 9 -185 15 -320 5 -135 17 -409 25 -610 9 -201 16 -394 18 -430
l2 -65 65 1 c119 1 708 43 965 69 77 7 211 19 298 26 98 8 154 16 148 22 -10
10 -208 -2 -526 -32 -110 -11 -292 -25 -405 -31 -113 -6 -252 -15 -310 -21
-58 -5 -130 -9 -160 -9 l-55 0 -2 63 c-3 71 -19 454 -34 797 -5 129 -14 340
-19 469 -10 236 -16 363 -25 546 -8 152 -14 339 -20 555 -3 107 -7 231 -10
275 -2 44 -7 134 -10 200 -4 66 -9 128 -11 138 -5 15 4 17 95 17 61 0 101 4
101 10 0 6 -35 10 -82 11 -85 0 -121 8 -118 24 1 5 -3 11 -9 13 -6 2 -11 -3
-11 -12 0 -13 -13 -16 -75 -16 -62 0 -73 2 -69 15 9 22 -3 18 -16 -4z"/>
<path d="M3075 4562 c-154 -52 -248 -139 -325 -305 -68 -145 -73 -282 -18
-450 43 -132 148 -295 255 -398 117 -111 196 -163 303 -198 325 -106 523 -20
651 284 5 11 16 58 25 105 18 93 15 278 -6 380 -16 76 -82 206 -131 260 -23
25 -34 34 -25 20 128 -201 156 -292 157 -505 0 -191 -48 -334 -149 -447 -100
-111 -260 -146 -447 -98 -149 38 -253 100 -374 221 -86 86 -132 148 -186 254
-57 113 -83 195 -91 293 -8 101 7 174 62 290 71 152 186 247 349 287 96 24
192 15 326 -28 189 -62 363 -188 418 -301 14 -29 20 -35 21 -20 0 26 -47 100
-91 141 -68 63 -191 142 -267 172 -201 77 -322 88 -457 43z"/>
<path d="M13 3879 c-16 -41 -18 -2719 -3 -2719 6 0 10 -5 10 -11 0 -5 -5 -7
-10 -4 -6 4 -10 -16 -10 -54 0 -38 4 -61 13 -64 9 -4 9 -6 -1 -6 -9 -1 -12
-49 -12 -208 1 -146 4 -203 11 -193 8 11 9 7 3 -15 -9 -40 -6 -77 7 -86 8 -4
7 -13 -2 -30 -18 -32 -21 -57 -8 -64 7 -5 6 -11 -2 -21 -6 -8 -7 -14 -2 -14 5
0 9 -18 9 -39 -1 -22 2 -37 6 -34 7 4 12 -15 11 -44 -1 -7 3 -10 8 -7 5 3 9 0
9 -5 0 -6 -3 -11 -7 -11 -13 0 -13 -133 0 -149 10 -12 9 -13 -3 -6 -13 8 -13
6 -2 -8 8 -9 11 -21 7 -27 -3 -5 -2 -10 4 -10 5 0 8 -11 6 -25 l-2 -25 165 0
c158 0 164 1 159 20 -3 10 0 21 5 22 12 4 21 58 9 58 -4 0 -7 9 -8 20 -1 13 2
18 10 13 7 -4 7 -1 -1 10 -8 9 -11 21 -7 27 4 6 1 18 -6 26 -10 12 -9 13 7 7
16 -6 16 -4 -3 28 -19 30 -19 36 -6 48 8 7 11 20 8 30 -12 29 -12 46 -1 53 8
5 7 8 -2 8 -7 0 -10 5 -7 10 3 6 3 10 -2 10 -9 0 -9 87 1 118 4 12 3 29 -3 39
-6 12 -6 25 0 36 6 9 7 23 2 30 -4 6 -6 22 -5 34 2 11 -1 24 -7 27 -6 4 -7 16
-3 29 5 12 8 29 7 37 -1 8 -3 28 -3 43 -1 18 -6 26 -13 21 -8 -4 -8 -1 -1 13
6 11 7 25 1 34 -4 9 -6 27 -3 40 12 50 5 203 -9 226 -4 6 -7 46 -7 90 0 75 -5
148 -10 148 -7 0 -7 106 1 110 5 4 9 11 9 16 0 6 -4 7 -10 4 -6 -3 -7 1 -4 10
4 8 1 22 -6 30 -7 8 -9 23 -4 37 10 29 11 107 2 102 -4 -2 -9 0 -12 5 -4 5 -2
12 4 16 6 4 8 11 4 16 -3 5 -7 71 -9 147 -2 75 -5 156 -7 180 -2 23 1 43 5 45
4 1 3 25 -4 52 -6 28 -13 118 -15 200 -2 83 -6 168 -9 190 -3 22 -6 78 -7 123
0 46 -3 79 -7 74 -3 -6 -6 51 -7 126 0 76 -4 193 -7 262 -4 69 -9 169 -11 223
-2 54 -9 104 -16 111 -6 8 -7 17 -2 19 8 6 7 81 -2 127 -3 14 -8 41 -10 60 -2
19 -9 48 -15 63 -6 15 -10 31 -9 35 6 22 -62 120 -80 114 -7 -2 -10 2 -6 12 5
14 -14 23 -31 14 -2 -2 -10 5 -17 15 -12 16 -14 15 -25 -14z m4 -296 c-3 -10
-5 -4 -5 12 0 17 2 24 5 18 2 -7 2 -21 0 -30z m10 -235 c-3 -7 -5 -2 -5 12 0
14 2 19 5 13 2 -7 2 -19 0 -25z m10 -195 c-3 -10 -5 -4 -5 12 0 17 2 24 5 18
2 -7 2 -21 0 -30z m10 -185 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1
-19z m10 -205 c-3 -10 -5 -4 -5 12 0 17 2 24 5 18 2 -7 2 -21 0 -30z m10 -175
c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1 -19z m10 -170 c-3 -7 -5 -2
-5 12 0 14 2 19 5 13 2 -7 2 -19 0 -25z m10 -195 c-3 -10 -5 -4 -5 12 0 17 2
24 5 18 2 -7 2 -21 0 -30z m10 -170 c-3 -10 -5 -4 -5 12 0 17 2 24 5 18 2 -7
2 -21 0 -30z m10 -170 c-3 -10 -5 -2 -5 17 0 19 2 27 5 18 2 -10 2 -26 0 -35z
m10 -190 c-3 -10 -5 -2 -5 17 0 19 2 27 5 18 2 -10 2 -26 0 -35z m-100 -145
c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1 -19z m110 -25 c-3 -10 -5 -4
-5 12 0 17 2 24 5 18 2 -7 2 -21 0 -30z m-110 -40 c-3 -10 -5 -2 -5 17 0 19 2
27 5 18 2 -10 2 -26 0 -35z m38 -94 c-4 -13 -1 -19 9 -19 12 0 16 -10 15 -37
-1 -28 -3 -31 -6 -13 -3 14 -14 34 -26 44 -13 13 -16 21 -8 24 9 3 9 7 0 18
-10 12 -9 13 5 8 11 -4 15 -13 11 -25z m-38 -31 c-3 -7 -5 -2 -5 12 0 14 2 19
5 13 2 -7 2 -19 0 -25z m120 0 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12
1 -19z m-101 -127 c0 -39 -3 -68 -8 -65 -10 6 -11 134 0 134 4 0 7 -31 8 -69z
m111 -23 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1 -19z m-62 -32 c-10
-40 -18 -55 -27 -50 -5 3 -5 11 2 19 6 7 10 17 9 21 -1 5 -1 18 0 29 1 15 3
17 10 6 5 -7 8 -19 6 -25z m32 -28 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4
-12 1 -19z m20 -40 c-3 -7 -5 -2 -5 12 0 14 2 19 5 13 2 -7 2 -19 0 -25z m-50
0 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1 -19z m-20 -50 c-3 -7 -5 -2
-5 12 0 14 2 19 5 13 2 -7 2 -19 0 -25z m90 1 c1 -19 1 -20 -4 -4 -3 11 -10
24 -17 28 -7 6 -6 7 4 4 8 -2 16 -14 17 -28z m-20 -31 c-3 -7 -5 -2 -5 12 0
14 2 19 5 13 2 -7 2 -19 0 -25z m-10 -150 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13
3 -3 4 -12 1 -19z m50 -140 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1
-19z m10 -155 c-3 -10 -5 -4 -5 12 0 17 2 24 5 18 2 -7 2 -21 0 -30z m-160
-25 c-3 -8 -6 -5 -6 6 -1 11 2 17 5 13 3 -3 4 -12 1 -19z m170 -130 c-2 -13
-4 -5 -4 17 -1 22 1 32 4 23 2 -10 2 -28 0 -40z m10 -125 c-3 -10 -5 -4 -5 12
0 17 2 24 5 18 2 -7 2 -21 0 -30z m10 -130 c-3 -10 -5 -4 -5 12 0 17 2 24 5
18 2 -7 2 -21 0 -30z m10 -120 c-3 -10 -5 -4 -5 12 0 17 2 24 5 18 2 -7 2 -21
0 -30z"/>
<path d="M5145 3860 c4 -6 11 -8 16 -5 14 9 11 15 -7 15 -8 0 -12 -5 -9 -10z"/>
<path d="M2995 2450 c3 -5 8 -10 11 -10 2 0 4 5 4 10 0 6 -5 10 -11 10 -5 0
-7 -4 -4 -10z"/>
<path d="M3621 1942 c-1 -67 3 -101 9 -97 6 3 10 26 10 50 l0 43 48 6 c26 3
103 8 172 12 69 3 142 7 162 9 20 2 100 7 176 11 l140 7 0 -34 c0 -19 10 -90
22 -159 12 -69 23 -136 26 -150 2 -14 7 -77 10 -140 l7 -115 -284 0 c-258 0
-405 -6 -498 -21 -37 -6 -36 5 -11 -133 5 -30 10 -167 10 -303 1 -188 4 -248
13 -248 10 0 12 61 9 263 -2 226 -8 306 -24 377 -6 25 69 31 447 37 204 2 385
7 403 10 28 5 34 2 39 -16 3 -11 10 -21 15 -21 5 0 6 9 3 20 -6 18 -2 20 47
20 74 0 291 11 303 15 10 2 7 75 -18 542 -4 65 -2 83 10 87 8 3 62 6 119 6 67
0 104 4 104 11 0 7 -36 9 -112 5 -344 -17 -378 -17 -378 -5 0 8 -4 7 -12 -3
-11 -14 -42 -18 -173 -23 -88 -3 -194 -7 -235 -10 -41 -2 -160 -9 -265 -15
-104 -6 -209 -12 -231 -15 l-41 -4 -10 42 c-10 38 -11 31 -12 -61z m1099 28
c0 -22 6 -79 14 -127 19 -113 51 -431 45 -437 -3 -3 -28 -7 -57 -10 l-51 -5
-7 34 c-3 19 -4 56 -3 83 2 28 1 48 -3 45 -5 -2 -8 20 -9 49 -1 67 -8 130 -21
188 -5 25 -11 59 -13 77 -1 17 -6 54 -10 81 l-7 51 34 4 c88 11 88 11 88 -33z
m117 33 c-3 -5 1 -120 9 -258 19 -362 20 -345 -15 -345 l-28 0 -7 98 c-4 53
-16 160 -28 237 -43 293 -44 275 23 275 28 0 48 -3 46 -7z m-251 -26 c1 -10 5
-48 8 -85 3 -36 10 -80 15 -97 9 -26 21 -143 38 -347 l5 -57 -56 1 c-30 1 -60
4 -65 7 -5 4 -13 81 -17 171 -3 91 -14 221 -23 290 l-17 125 40 6 c58 9 69 6
72 -14z m-126 1 c0 -7 6 -56 14 -108 8 -52 18 -182 21 -287 l7 -193 -39 0 -39
0 -12 143 c-7 78 -21 185 -31 237 -11 52 -22 120 -26 152 -7 63 -5 65 68 67
23 1 37 -4 37 -11z"/>
<path d="M2720 1938 c1 -13 7 -45 15 -73 15 -55 20 -31 9 49 -7 47 -24 65 -24
24z"/>
<path d="M1005 1670 c-3 -5 -1 -10 4 -10 6 0 11 5 11 10 0 6 -2 10 -4 10 -3 0
-8 -4 -11 -10z"/>
<path d="M11 284 c0 -11 3 -14 6 -6 3 7 2 16 -1 19 -3 4 -6 -2 -5 -13z"/>
</g>
</svg>

如果ID为12的文档存在,我想将新的价格对象推送到“ price_history”数组中。如果没有,我将创建一个与粘贴的代码相同的新文档。

这似乎很简单,但是我检查了多个stackoverflow主题和mongodb文档,但无法获取它:/

我想出了代码:

{"vendor_id": 12, "title": "xyz", "price": 1499.0, "price_history": [{"date": "2019-12-01", "price": 1890.0}]}

但是当找不到文档时,它仅插入vendor_id而不是整个文档。

有什么提示吗? 谢谢您花费时间解决我的问题。

2 个答案:

答案 0 :(得分:2)

$setOnInsert进行救援:

db.holidays.update(
   { "vendor_id": t["vendor_id" },   // Query parameter
   ,{                     // Update document
      "$push": {"price_history": t["price_history"][0]},
      "$setOnInsert": { everything else you want insert besides the push and the vendor_id
   }
   ,{ upsert: true }      // Options
)

答案 1 :(得分:1)

将记录提取到字典中,并使用标准python进行操纵。如果您使用find_one()并且没有匹配项,它将返回None

from pymongo import MongoClient
from bson.json_util import dumps

db = MongoClient()["testdatabase"]

# Data setup
db.testcollection.delete_many({})
template = {"vendor_id": 12, "title": "xyz", "price": 1499.0, "price_history": []}
data_setup = {"vendor_id": 12, "title": "xyz", "price": 1499.0,
              "price_history": [{"date": "2019-12-01", "price": 1890.0}]}
new_price = {"date": "2019-12-02", "price": 2000.0}

# Comment the next line out to see what happens if the record isn't present
db.testcollection.insert_one(data_setup)

record = db.testcollection.find_one({"vendor_id": 12})
if record is None:
    record = template

record['price_history'].append(new_price)
db.testcollection.replace_one({"vendor_id": 12}, record, upsert=True)

# Pretty up the record output
print(dumps(db.testcollection.find_one({}, {'_id': 0}), indent=4))

给予:

{
    "vendor_id": 12,
    "title": "xyz",
    "price": 1499.0,
    "price_history": [
        {
            "date": "2019-12-01",
            "price": 1890.0
        },
        {
            "date": "2019-12-02",
            "price": 2000.0
        }
    ]
}