w-orm-mongodb 1.1.43 → 1.1.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,7 +83,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
83
83
 
84
84
  <dt class="tag-source">Source:</dt>
85
85
  <dd class="tag-source"><ul class="dummy"><li>
86
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line37">line 37</a>
86
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line39">line 39</a>
87
87
  </li></ul></dd>
88
88
 
89
89
 
@@ -474,7 +474,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
474
474
 
475
475
  <dt class="tag-source">Source:</dt>
476
476
  <dd class="tag-source"><ul class="dummy"><li>
477
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line936">line 936</a>
477
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line980">line 980</a>
478
478
  </li></ul></dd>
479
479
 
480
480
 
@@ -636,7 +636,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
636
636
 
637
637
  <dt class="tag-source">Source:</dt>
638
638
  <dd class="tag-source"><ul class="dummy"><li>
639
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1064">line 1064</a>
639
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1108">line 1108</a>
640
640
  </li></ul></dd>
641
641
 
642
642
 
@@ -816,7 +816,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
816
816
 
817
817
  <dt class="tag-source">Source:</dt>
818
818
  <dd class="tag-source"><ul class="dummy"><li>
819
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1518">line 1518</a>
819
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1562">line 1562</a>
820
820
  </li></ul></dd>
821
821
 
822
822
 
@@ -996,7 +996,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
996
996
 
997
997
  <dt class="tag-source">Source:</dt>
998
998
  <dd class="tag-source"><ul class="dummy"><li>
999
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1374">line 1374</a>
999
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1418">line 1418</a>
1000
1000
  </li></ul></dd>
1001
1001
 
1002
1002
 
@@ -1141,7 +1141,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
1141
1141
 
1142
1142
 
1143
1143
 
1144
- <h4 class="name" id=".insert"><span class="type-signature type-signature-async, static">(async, static) </span>insert<span class="signature">(data)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
1144
+ <h4 class="name" id=".insert"><span class="type-signature type-signature-async, static">(async, static) </span>insert<span class="signature">(data, option<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
1145
1145
 
1146
1146
 
1147
1147
 
@@ -1153,14 +1153,16 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
1153
1153
  <dt class="tag-description">Description:</dt>
1154
1154
  <dd class="tag-description"><ul class="dummy"><li><p>插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
1155
1155
  由MongoDB於唯一索引上原子完成[檢查id未存在]與[寫入],併發時同一id僅有一次成功
1156
- 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
1156
+ 註: 預設回傳單一聚合物件,n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
1157
+ 註: option.returnList為true時改回與輸入等長且保序之逐筆陣列,供呼叫端得知[是哪幾筆]為新數據,
1158
+ 聚合計數僅能回答[有幾筆],而去重類呼叫端須知何者為新方能對其執行下游動作
1157
1159
  註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入</p></li></ul></dd>
1158
1160
 
1159
1161
 
1160
1162
 
1161
1163
  <dt class="tag-source">Source:</dt>
1162
1164
  <dd class="tag-source"><ul class="dummy"><li>
1163
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line426">line 426</a>
1165
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line432">line 432</a>
1164
1166
  </li></ul></dd>
1165
1167
 
1166
1168
 
@@ -1220,8 +1222,12 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
1220
1222
  <th>Type</th>
1221
1223
 
1222
1224
 
1225
+ <th>Attributes</th>
1226
+
1223
1227
 
1224
1228
 
1229
+ <th>Default</th>
1230
+
1225
1231
 
1226
1232
  <th class="last">Description</th>
1227
1233
  </tr>
@@ -1249,13 +1255,135 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
1249
1255
  </td>
1250
1256
 
1251
1257
 
1258
+ <td class="attributes">
1259
+
1260
+
1261
+
1262
+
1263
+
1264
+ </td>
1265
+
1252
1266
 
1253
1267
 
1268
+ <td class="default">
1269
+
1270
+ </td>
1271
+
1254
1272
 
1255
1273
  <td class="description last"><p>輸入數據物件或陣列</p></td>
1256
1274
  </tr>
1257
1275
 
1258
1276
 
1277
+
1278
+ <tr>
1279
+
1280
+ <td class="name"><code>option</code></td>
1281
+
1282
+
1283
+ <td class="type">
1284
+
1285
+
1286
+ <span class="param-type">Object</span>
1287
+
1288
+
1289
+
1290
+
1291
+ </td>
1292
+
1293
+
1294
+ <td class="attributes">
1295
+
1296
+ &lt;optional><br>
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+ </td>
1303
+
1304
+
1305
+
1306
+ <td class="default">
1307
+
1308
+ <code>{}</code>
1309
+
1310
+ </td>
1311
+
1312
+
1313
+ <td class="description last"><p>輸入設定物件,預設為{}</p>
1314
+ <h6>Properties</h6>
1315
+
1316
+
1317
+ <table class="params">
1318
+ <thead>
1319
+ <tr>
1320
+
1321
+ <th>Name</th>
1322
+
1323
+
1324
+ <th>Type</th>
1325
+
1326
+
1327
+ <th>Attributes</th>
1328
+
1329
+
1330
+
1331
+ <th>Default</th>
1332
+
1333
+
1334
+ <th class="last">Description</th>
1335
+ </tr>
1336
+ </thead>
1337
+
1338
+ <tbody>
1339
+
1340
+
1341
+ <tr>
1342
+
1343
+ <td class="name"><code>returnList</code></td>
1344
+
1345
+
1346
+ <td class="type">
1347
+
1348
+
1349
+ <span class="param-type">Boolean</span>
1350
+
1351
+
1352
+
1353
+
1354
+ </td>
1355
+
1356
+
1357
+ <td class="attributes">
1358
+
1359
+ &lt;optional><br>
1360
+
1361
+
1362
+
1363
+
1364
+
1365
+ </td>
1366
+
1367
+
1368
+
1369
+ <td class="default">
1370
+
1371
+ <code>false</code>
1372
+
1373
+ </td>
1374
+
1375
+
1376
+ <td class="description last"><p>輸入是否改回逐筆結果陣列,預設為false。為true時回傳與輸入等長且保序之陣列,各元素為{ n, nInserted, ok },已插入者nInserted為1、已存在而跳過者為0</p></td>
1377
+ </tr>
1378
+
1379
+
1380
+ </tbody>
1381
+ </table>
1382
+
1383
+ </td>
1384
+ </tr>
1385
+
1386
+
1259
1387
  </tbody>
1260
1388
  </table>
1261
1389
 
@@ -1278,7 +1406,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
1278
1406
 
1279
1407
 
1280
1408
  <div class="param-desc">
1281
- <p>回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息</p>
1409
+ <p>回傳Promise,resolve回傳插入結果,returnList為true時回傳逐筆結果陣列,reject回傳錯誤訊息</p>
1282
1410
  </div>
1283
1411
 
1284
1412
 
@@ -1328,7 +1456,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
1328
1456
 
1329
1457
  <dt class="tag-source">Source:</dt>
1330
1458
  <dd class="tag-source"><ul class="dummy"><li>
1331
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line638">line 638</a>
1459
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line682">line 682</a>
1332
1460
  </li></ul></dd>
1333
1461
 
1334
1462
 
@@ -1492,7 +1620,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
1492
1620
 
1493
1621
  <dt class="tag-source">Source:</dt>
1494
1622
  <dd class="tag-source"><ul class="dummy"><li>
1495
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1128">line 1128</a>
1623
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1172">line 1172</a>
1496
1624
  </li></ul></dd>
1497
1625
 
1498
1626
 
@@ -1656,7 +1784,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
1656
1784
 
1657
1785
  <dt class="tag-source">Source:</dt>
1658
1786
  <dd class="tag-source"><ul class="dummy"><li>
1659
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line856">line 856</a>
1787
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line900">line 900</a>
1660
1788
  </li></ul></dd>
1661
1789
 
1662
1790
 
@@ -1943,7 +2071,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
1943
2071
 
1944
2072
  <dt class="tag-source">Source:</dt>
1945
2073
  <dd class="tag-source"><ul class="dummy"><li>
1946
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line305">line 305</a>
2074
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line307">line 307</a>
1947
2075
  </li></ul></dd>
1948
2076
 
1949
2077
 
@@ -2123,7 +2251,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
2123
2251
 
2124
2252
  <dt class="tag-source">Source:</dt>
2125
2253
  <dd class="tag-source"><ul class="dummy"><li>
2126
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line362">line 362</a>
2254
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line364">line 364</a>
2127
2255
  </li></ul></dd>
2128
2256
 
2129
2257
 
@@ -2284,7 +2412,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
2284
2412
 
2285
2413
  <dt class="tag-source">Source:</dt>
2286
2414
  <dd class="tag-source"><ul class="dummy"><li>
2287
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1238">line 1238</a>
2415
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1282">line 1282</a>
2288
2416
  </li></ul></dd>
2289
2417
 
2290
2418
 
@@ -2441,7 +2569,7 @@ standalone無交易可用而以補償動作達成,其限制詳見_insertBulkBy
2441
2569
  <br class="clear">
2442
2570
 
2443
2571
  <footer>
2444
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 11:28:35 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
2572
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 18:43:44 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
2445
2573
  </footer>
2446
2574
 
2447
2575
  <script>prettyPrint();</script>
@@ -48,6 +48,7 @@
48
48
  <pre class="prettyprint source linenums"><code>import mongodb from 'mongodb'
49
49
  import stream from 'stream'
50
50
  import cloneDeep from 'lodash-es/cloneDeep.js'
51
+ import each from 'lodash-es/each.js'
51
52
  import every from 'lodash-es/every.js'
52
53
  import filter from 'lodash-es/filter.js'
53
54
  import get from 'lodash-es/get.js'
@@ -62,6 +63,7 @@ import isestr from 'wsemi/src/isestr.mjs'
62
63
  import isarr from 'wsemi/src/isarr.mjs'
63
64
  import isearr from 'wsemi/src/isearr.mjs'
64
65
  import iseobj from 'wsemi/src/iseobj.mjs'
66
+ import isnum from 'wsemi/src/isnum.mjs'
65
67
  import isu8arr from 'wsemi/src/isu8arr.mjs'
66
68
  import pmSeries from 'wsemi/src/pmSeries.mjs'
67
69
 
@@ -463,18 +465,31 @@ function WOrmMongodb(opt = {}) {
463
465
  /**
464
466
  * 插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
465
467
  * 由MongoDB於唯一索引上原子完成[檢查id未存在]與[寫入],併發時同一id僅有一次成功
466
- * 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
468
+ * 註: 預設回傳單一聚合物件,n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
469
+ * 註: option.returnList為true時改回與輸入等長且保序之逐筆陣列,供呼叫端得知[是哪幾筆]為新數據,
470
+ * 聚合計數僅能回答[有幾筆],而去重類呼叫端須知何者為新方能對其執行下游動作
467
471
  * 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
468
472
  *
469
473
  * @memberOf WOrmMongodb
470
474
  * @param {Object|Array} data 輸入數據物件或陣列
471
- * @returns {Promise} 回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息
475
+ * @param {Object} [option={}] 輸入設定物件,預設為{}
476
+ * @param {Boolean} [option.returnList=false] 輸入是否改回逐筆結果陣列,預設為false。為true時回傳與輸入等長且保序之陣列,各元素為{ n, nInserted, ok },已插入者nInserted為1、已存在而跳過者為0
477
+ * @returns {Promise} 回傳Promise,resolve回傳插入結果,returnList為true時回傳逐筆結果陣列,reject回傳錯誤訊息
472
478
  */
473
- async function insert(data) {
479
+ async function insert(data, option = {}) {
474
480
  let isErr = false
475
481
 
482
+ //returnList
483
+ let returnList = get(option, 'returnList')
484
+ if (!isbol(returnList)) {
485
+ returnList = false
486
+ }
487
+
476
488
  //check
477
489
  if (!iseobj(data) &amp;&amp; !isearr(data)) {
490
+ if (returnList) {
491
+ return []
492
+ }
478
493
  return {
479
494
  n: 0,
480
495
  nInserted: 0,
@@ -515,7 +530,10 @@ function WOrmMongodb(opt = {}) {
515
530
 
516
531
  //insertMany, ordered:false令已存在id者跳過而不中斷整批插入,
517
532
  //同批含重複id時亦僅首筆成功, 故不須逐筆插入即可取得實際插入筆數
533
+ //ordered:false時writeErrors之index即為未插入者於輸入內之位置, 其餘即為已插入者,
534
+ //故逐筆判定亦由本次同一往返取得, 不須額外查詢
518
535
  let nInserted = 0
536
+ let kpSkip = {}
519
537
  try {
520
538
  let r = await collection.insertMany(data, { ordered: false })
521
539
  nInserted = r.insertedCount
@@ -530,13 +548,39 @@ function WOrmMongodb(opt = {}) {
530
548
  //重複鍵錯誤時仍可由result取得實際插入筆數
531
549
  nInserted = get(err, 'result.insertedCount', 0)
532
550
 
551
+ //kpSkip, 記錄未插入者於輸入內之位置
552
+ each(get(err, 'writeErrors', []), function(v) {
553
+ let k = get(v, 'index', get(v, 'err.index'))
554
+ if (isnum(k)) {
555
+ kpSkip[k] = true
556
+ }
557
+ })
558
+
533
559
  }
534
560
 
535
- //res, 全數已存在而nInserted為0屬正常結果, 不視為錯誤
536
- res = {
537
- n: nAll,
538
- nInserted,
539
- ok: 1,
561
+ //res
562
+ if (returnList) {
563
+
564
+ //res, 與輸入等長且保序, 各元素之n恆為1、ok恆為1,
565
+ //因insert之任何錯誤皆屬整批性錯誤而reject, 逐筆元素不出現ok為0與err
566
+ res = map(data, function(v, k) {
567
+ return {
568
+ n: 1,
569
+ nInserted: kpSkip[k] === true ? 0 : 1,
570
+ ok: 1,
571
+ }
572
+ })
573
+
574
+ }
575
+ else {
576
+
577
+ //res, 全數已存在而nInserted為0屬正常結果, 不視為錯誤
578
+ res = {
579
+ n: nAll,
580
+ nInserted,
581
+ ok: 1,
582
+ }
583
+
540
584
  }
541
585
 
542
586
  }
@@ -1663,7 +1707,7 @@ export default WOrmMongodb
1663
1707
  <br class="clear">
1664
1708
 
1665
1709
  <footer>
1666
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 11:28:35 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1710
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 18:43:44 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1667
1711
  </footer>
1668
1712
 
1669
1713
  <script>prettyPrint();</script>
package/docs/index.html CHANGED
@@ -71,7 +71,7 @@
71
71
  <br class="clear">
72
72
 
73
73
  <footer>
74
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 11:28:35 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
74
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 18:43:44 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
75
75
  </footer>
76
76
 
77
77
  <script>prettyPrint();</script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "w-orm-mongodb",
3
- "version": "1.1.43",
3
+ "version": "1.1.44",
4
4
  "main": "dist/w-orm-mongodb.umd.js",
5
5
  "dependencies": {
6
6
  "mongodb": "^7.5.0",
@@ -1,6 +1,7 @@
1
1
  import mongodb from 'mongodb'
2
2
  import stream from 'stream'
3
3
  import cloneDeep from 'lodash-es/cloneDeep.js'
4
+ import each from 'lodash-es/each.js'
4
5
  import every from 'lodash-es/every.js'
5
6
  import filter from 'lodash-es/filter.js'
6
7
  import get from 'lodash-es/get.js'
@@ -15,6 +16,7 @@ import isestr from 'wsemi/src/isestr.mjs'
15
16
  import isarr from 'wsemi/src/isarr.mjs'
16
17
  import isearr from 'wsemi/src/isearr.mjs'
17
18
  import iseobj from 'wsemi/src/iseobj.mjs'
19
+ import isnum from 'wsemi/src/isnum.mjs'
18
20
  import isu8arr from 'wsemi/src/isu8arr.mjs'
19
21
  import pmSeries from 'wsemi/src/pmSeries.mjs'
20
22
 
@@ -416,18 +418,31 @@ function WOrmMongodb(opt = {}) {
416
418
  /**
417
419
  * 插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
418
420
  * 由MongoDB於唯一索引上原子完成[檢查id未存在]與[寫入],併發時同一id僅有一次成功
419
- * 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
421
+ * 註: 預設回傳單一聚合物件,n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
422
+ * 註: option.returnList為true時改回與輸入等長且保序之逐筆陣列,供呼叫端得知[是哪幾筆]為新數據,
423
+ * 聚合計數僅能回答[有幾筆],而去重類呼叫端須知何者為新方能對其執行下游動作
420
424
  * 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
421
425
  *
422
426
  * @memberOf WOrmMongodb
423
427
  * @param {Object|Array} data 輸入數據物件或陣列
424
- * @returns {Promise} 回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息
428
+ * @param {Object} [option={}] 輸入設定物件,預設為{}
429
+ * @param {Boolean} [option.returnList=false] 輸入是否改回逐筆結果陣列,預設為false。為true時回傳與輸入等長且保序之陣列,各元素為{ n, nInserted, ok },已插入者nInserted為1、已存在而跳過者為0
430
+ * @returns {Promise} 回傳Promise,resolve回傳插入結果,returnList為true時回傳逐筆結果陣列,reject回傳錯誤訊息
425
431
  */
426
- async function insert(data) {
432
+ async function insert(data, option = {}) {
427
433
  let isErr = false
428
434
 
435
+ //returnList
436
+ let returnList = get(option, 'returnList')
437
+ if (!isbol(returnList)) {
438
+ returnList = false
439
+ }
440
+
429
441
  //check
430
442
  if (!iseobj(data) && !isearr(data)) {
443
+ if (returnList) {
444
+ return []
445
+ }
431
446
  return {
432
447
  n: 0,
433
448
  nInserted: 0,
@@ -468,7 +483,10 @@ function WOrmMongodb(opt = {}) {
468
483
 
469
484
  //insertMany, ordered:false令已存在id者跳過而不中斷整批插入,
470
485
  //同批含重複id時亦僅首筆成功, 故不須逐筆插入即可取得實際插入筆數
486
+ //ordered:false時writeErrors之index即為未插入者於輸入內之位置, 其餘即為已插入者,
487
+ //故逐筆判定亦由本次同一往返取得, 不須額外查詢
471
488
  let nInserted = 0
489
+ let kpSkip = {}
472
490
  try {
473
491
  let r = await collection.insertMany(data, { ordered: false })
474
492
  nInserted = r.insertedCount
@@ -483,13 +501,39 @@ function WOrmMongodb(opt = {}) {
483
501
  //重複鍵錯誤時仍可由result取得實際插入筆數
484
502
  nInserted = get(err, 'result.insertedCount', 0)
485
503
 
504
+ //kpSkip, 記錄未插入者於輸入內之位置
505
+ each(get(err, 'writeErrors', []), function(v) {
506
+ let k = get(v, 'index', get(v, 'err.index'))
507
+ if (isnum(k)) {
508
+ kpSkip[k] = true
509
+ }
510
+ })
511
+
486
512
  }
487
513
 
488
- //res, 全數已存在而nInserted為0屬正常結果, 不視為錯誤
489
- res = {
490
- n: nAll,
491
- nInserted,
492
- ok: 1,
514
+ //res
515
+ if (returnList) {
516
+
517
+ //res, 與輸入等長且保序, 各元素之n恆為1、ok恆為1,
518
+ //因insert之任何錯誤皆屬整批性錯誤而reject, 逐筆元素不出現ok為0與err
519
+ res = map(data, function(v, k) {
520
+ return {
521
+ n: 1,
522
+ nInserted: kpSkip[k] === true ? 0 : 1,
523
+ ok: 1,
524
+ }
525
+ })
526
+
527
+ }
528
+ else {
529
+
530
+ //res, 全數已存在而nInserted為0屬正常結果, 不視為錯誤
531
+ res = {
532
+ n: nAll,
533
+ nInserted,
534
+ ok: 1,
535
+ }
536
+
493
537
  }
494
538
 
495
539
  }