3939from pymongo .helpers import _check_write_command_response , _command
4040from pymongo .message import _INSERT , _UPDATE , _DELETE
4141from pymongo .read_preferences import ReadPreference
42+ from pymongo .write_concern import WriteConcern
4243
4344
4445try :
5051_NO_OBJ_ERROR = "No matching object found"
5152
5253
54+ class InsertOneResult (object ):
55+ """The return type for :meth:`Collection.insert_one`."""
56+
57+ __slots__ = ("__inserted_id" , "__acknowledged" )
58+
59+ def __init__ (self , inserted_id , acknowledged ):
60+ self .__inserted_id = inserted_id
61+ self .__acknowledged = acknowledged
62+
63+ @property
64+ def inserted_id (self ):
65+ """The inserted document's _id."""
66+ return self .__inserted_id
67+
68+ @property
69+ def acknowledged (self ):
70+ """Is this the result of an acknowledged write operation?"""
71+ return self .__acknowledged
72+
73+
5374class ReturnDocument (object ):
5475 """An enum used with :meth:`Collection.find_one_and_replace` and
5576 :meth:`Collection.find_one_and_update`.
@@ -483,8 +504,34 @@ def insert(self, doc_or_docs, manipulate=True,
483504
484505 .. mongodoc:: insert
485506 """
507+ write_concern = None
508+ if kwargs :
509+ write_concern = WriteConcern (** kwargs )
510+ return self .__insert (doc_or_docs , not continue_on_error ,
511+ check_keys , manipulate , write_concern )
512+
513+ def insert_one (self , document ):
514+ """Insert a single document.
515+
516+ :Parameters:
517+ - `document`: The document to insert. Must be a mutable mapping
518+ type. If the document does not have an _id field one will be
519+ added automatically.
520+
521+ :Returns:
522+ - An instance of :class:`InsertOneResult`.
523+ """
524+ if not isinstance (document , collections .MutableMapping ):
525+ raise TypeError ("document must be a mutable mapping type" )
526+ if "_id" not in document :
527+ document ["_id" ] = ObjectId ()
528+ return InsertOneResult (self .__insert (document ),
529+ self .write_concern .acknowledged )
530+
531+ def __insert (self , docs , ordered = True ,
532+ check_keys = True , manipulate = False , write_concern = None ):
533+ """Internal insert helper."""
486534 client = self .database .connection
487- docs = doc_or_docs
488535 return_one = False
489536 if isinstance (docs , collections .MutableMapping ):
490537 return_one = True
@@ -512,13 +559,13 @@ def gen():
512559 ids .append (doc .get ('_id' ))
513560 yield doc
514561
515- concern = kwargs or self .write_concern .document
562+ concern = ( write_concern or self .write_concern ) .document
516563 safe = concern .get ("w" ) != 0
517564
518565 if client ._writable_max_wire_version () > 1 and safe :
519566 # Insert command
520567 command = SON ([('insert' , self .name ),
521- ('ordered' , not continue_on_error )])
568+ ('ordered' , ordered )])
522569
523570 if concern :
524571 command ['writeConcern' ] = concern
@@ -530,9 +577,8 @@ def gen():
530577 else :
531578 # Legacy batched OP_INSERT
532579 message ._do_batched_insert (self .__full_name , gen (), check_keys ,
533- safe , concern , continue_on_error ,
580+ safe , concern , not ordered ,
534581 self .codec_options , client )
535-
536582 if return_one :
537583 return ids [0 ]
538584 else :
0 commit comments