@@ -293,7 +293,12 @@ def first(self):
293
293
return result
294
294
295
295
def insert (
296
- self , doc_or_docs , load_bulk = True , write_concern = None , signal_kwargs = None
296
+ self ,
297
+ doc_or_docs ,
298
+ load_bulk = True ,
299
+ write_concern = None ,
300
+ signal_kwargs = None ,
301
+ ordered = True ,
297
302
):
298
303
"""bulk insert documents
299
304
@@ -309,6 +314,11 @@ def insert(
309
314
each server being written to.
310
315
:param signal_kwargs: (optional) kwargs dictionary to be passed to
311
316
the signal calls.
317
+ :param ordered (optional): If True (the default) documents will be
318
+ inserted on the server serially, in the order provided. If an error
319
+ occurs all remaining inserts are aborted. If False, documents will
320
+ be inserted on the server in arbitrary order, possibly in parallel,
321
+ and all document inserts will be attempted.
312
322
313
323
By default returns document instances, set ``load_bulk`` to False to
314
324
return just ``ObjectIds``
@@ -341,12 +351,14 @@ def insert(
341
351
342
352
with set_write_concern (self ._collection , write_concern ) as collection :
343
353
insert_func = collection .insert_many
354
+ insert_func_kwargs = {"ordered" : ordered }
344
355
if return_one :
345
356
raw = raw [0 ]
346
357
insert_func = collection .insert_one
358
+ insert_func_kwargs = {}
347
359
348
360
try :
349
- inserted_result = insert_func (raw )
361
+ inserted_result = insert_func (raw , ** insert_func_kwargs )
350
362
ids = (
351
363
[inserted_result .inserted_id ]
352
364
if return_one
@@ -358,6 +370,17 @@ def insert(
358
370
except pymongo .errors .BulkWriteError as err :
359
371
# inserting documents that already have an _id field will
360
372
# give huge performance debt or raise
373
+ if ordered :
374
+ inserted = err .details ["nInserted" ]
375
+ for doc , raw_doc in zip (docs [:inserted ], raw [:inserted ]):
376
+ doc .pk = raw_doc ["_id" ]
377
+ else :
378
+ not_writed_ids = [
379
+ error ["op" ]["_id" ] for error in err .details ["writeErrors" ]
380
+ ]
381
+ for doc , raw_doc in zip (docs , raw ):
382
+ if raw_doc ["_id" ] not in not_writed_ids :
383
+ doc .pk = raw_doc ["_id" ]
361
384
message = "Bulk write error: (%s)"
362
385
raise BulkWriteError (message % err .details )
363
386
except pymongo .errors .OperationFailure as err :
@@ -1715,29 +1738,29 @@ def no_dereference(self):
1715
1738
1716
1739
def _item_frequencies_map_reduce (self , field , normalize = False ):
1717
1740
map_func = """
1718
- function() {
1719
- var path = '{{~%( field)s }}'.split('.');
1741
+ function() {{
1742
+ var path = '{{{{~{ field}}} }}'.split('.');
1720
1743
var field = this;
1721
1744
1722
- for (p in path) {
1745
+ for (p in path) {{
1723
1746
if (typeof field != 'undefined')
1724
1747
field = field[path[p]];
1725
1748
else
1726
1749
break;
1727
- }
1728
- if (field && field.constructor == Array) {
1729
- field.forEach(function(item) {
1750
+ }}
1751
+ if (field && field.constructor == Array) {{
1752
+ field.forEach(function(item) {{
1730
1753
emit(item, 1);
1731
- });
1732
- } else if (typeof field != 'undefined') {
1754
+ }} );
1755
+ }} else if (typeof field != 'undefined') { {
1733
1756
emit(field, 1);
1734
- } else {
1757
+ }} else { {
1735
1758
emit(null, 1);
1736
- }
1737
- }
1738
- """ % {
1739
- " field" : field
1740
- }
1759
+ }}
1760
+ }}
1761
+ """ . format (
1762
+ field = field
1763
+ )
1741
1764
reduce_func = """
1742
1765
function(key, values) {
1743
1766
var total = 0;
0 commit comments