MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / insert

Method insert

mongoengine/queryset/base.py:306–398  ·  view source on GitHub ↗

bulk insert documents :param doc_or_docs: a document or list of documents to be inserted :param load_bulk (optional): If True returns the list of document instances :param write_concern: Extra keyword arguments are passed down to :meth:`~pymongo.c

(
        self, doc_or_docs, load_bulk=True, write_concern=None, signal_kwargs=None
    )

Source from the content-addressed store, hash-verified

304 return result
305
306 def insert(
307 self, doc_or_docs, load_bulk=True, write_concern=None, signal_kwargs=None
308 ):
309 """bulk insert documents
310
311 :param doc_or_docs: a document or list of documents to be inserted
312 :param load_bulk (optional): If True returns the list of document
313 instances
314 :param write_concern: Extra keyword arguments are passed down to
315 :meth:`~pymongo.collection.Collection.insert`
316 which will be used as options for the resultant
317 ``getLastError`` command. For example,
318 ``insert(..., {w: 2, fsync: True})`` will wait until at least
319 two servers have recorded the write and will force an fsync on
320 each server being written to.
321 :param signal_kwargs: (optional) kwargs dictionary to be passed to
322 the signal calls.
323
324 By default returns document instances, set ``load_bulk`` to False to
325 return just ``ObjectIds``
326 """
327 Document = _import_class("Document")
328
329 if write_concern is None:
330 write_concern = {}
331
332 docs = doc_or_docs
333 return_one = False
334 if isinstance(docs, Document) or issubclass(docs.__class__, Document):
335 return_one = True
336 docs = [docs]
337
338 for doc in docs:
339 if not isinstance(doc, self._document):
340 msg = "Some documents inserted aren't instances of %s" % str(
341 self._document
342 )
343 raise OperationError(msg)
344 if doc.pk and not doc._created:
345 msg = "Some documents have ObjectIds, use doc.update() instead"
346 raise OperationError(msg)
347
348 signal_kwargs = signal_kwargs or {}
349 signals.pre_bulk_insert.send(self._document, documents=docs, **signal_kwargs)
350
351 raw = [doc.to_mongo() for doc in docs]
352
353 with set_write_concern(self._collection, write_concern) as collection:
354 insert_func = collection.insert_many
355 if return_one:
356 raw = raw[0]
357 insert_func = collection.insert_one
358
359 try:
360 inserted_result = insert_func(raw)
361 ids = (
362 [inserted_result.inserted_id]
363 if return_one

Callers 15

sumMethod · 0.80
averageMethod · 0.80
queryFunction · 0.80
updateFunction · 0.80
_build_index_specMethod · 0.80
bulk_create_authorMethod · 0.80
bulk_set_active_postMethod · 0.80

Calls 8

in_bulkMethod · 0.95
_import_classFunction · 0.90
OperationErrorClass · 0.90
set_write_concernFunction · 0.90
NotUniqueErrorClass · 0.90
BulkWriteErrorClass · 0.90
to_mongoMethod · 0.45
getMethod · 0.45