diff --git a/reference/mongo/mongocollection/batchinsert.xml b/reference/mongo/mongocollection/batchinsert.xml index 0d4cbac853..e96d155760 100644 --- a/reference/mongo/mongocollection/batchinsert.xml +++ b/reference/mongo/mongocollection/batchinsert.xml @@ -26,8 +26,17 @@ - An array of arrays. + An array of arrays or objects. If any objects are used, they may not have + protected or private properties. + + + If the documents to insert do not have an _id key or + property, a new MongoId instance will be created + and assigned to it. See MongoCollection::insert for + additional information on this behavior. + + @@ -76,6 +85,11 @@ &reftitle.errors; + + Throws MongoException if any inserted documents are + empty or if they contains zero-length keys. Attempting to insert an object + with protected and private properties will cause a zero-length key error. + &mongo.errors.exceptions.writeconcern; diff --git a/reference/mongo/mongocollection/insert.xml b/reference/mongo/mongocollection/insert.xml index 0bc8158d0e..ea4e398317 100644 --- a/reference/mongo/mongocollection/insert.xml +++ b/reference/mongo/mongocollection/insert.xml @@ -4,14 +4,14 @@ MongoCollection::insert - Inserts an array into the collection + Inserts a document into the collection &reftitle.description; public bool|arrayMongoCollection::insert - arraya + array|objecta arrayoptionsarray() @@ -31,8 +31,17 @@ - An array. + An array or object. If an object is used, it may not have protected or + private properties. + + + If the parameter does not have an _id key or + property, a new MongoId instance will be created + and assigned to it. This special behavior does not mean that the + parameter is passed by reference. + + @@ -188,7 +197,9 @@ &reftitle.errors; - Throws MongoException if the inserted array is empty. + Throws MongoException if the inserted document is + empty or if it contains zero-length keys. Attempting to insert an object with + protected and private properties will cause a zero-length key error. &mongo.errors.exceptions.writeconcern; @@ -265,27 +276,54 @@ &reftitle.examples; - <function>MongoCollection::insert</function> _id example + <function>MongoCollection::insert</function> <literal>_id</literal> example - Inserting an object will add an _id field to it, unless it is passed by reference. + An _id field will be added to the inserted document if + not already present. Depending on how the parameter is passed, a generated + _id may or may not be available to calling code. selectDB('test'); -$collection = new MongoCollection($db, 'phpmanual'); +$collection = $m->selectCollection('test', 'phpmanual'); -$a = array('x' => 12); +// If an array literal is used, there is no way to access the generated _id +$collection->insert(array('x' => 1)); + +// The _id is available on an array passed by value +$a = array('x' => 2); $collection->insert($a); var_dump($a); -$b = array('x' => 12); +// The _id is not available on an array passed by reference +$b = array('x' => 3); $ref = &$b; $collection->insert($ref); var_dump($ref); +// The _id is available if a wrapping function does not trigger copy-on-write +function insert_no_cow($collection, $document) +{ + $collection->insert($document); +} + +$c = array('x' => 4); +insert_no_cow($collection, $c); +var_dump($c); + +// The _id is not available if a wrapping function triggers copy-on-write +function insert_cow($collection, $document) +{ + $document['y'] = 1; + $collection->insert($document); +} + +$d = array('x' => 5); +insert_cow($collection, $d); +var_dump($d); + ?> ]]> @@ -294,14 +332,25 @@ var_dump($ref); - int(12) + int(2) ["_id"]=> object(MongoId)#4 (0) { } } -array(12) { +array(1) { ["x"]=> - int(1) + int(3) +} +array(2) { + ["x"]=> + int(4) + ["_id"]=> + object(MongoId)#5 (0) { + } +} +array(1) { + ["x"]=> + int(5) } ]]> diff --git a/reference/mongo/mongocollection/save.xml b/reference/mongo/mongocollection/save.xml index 010307dfac..b865ed2d2e 100644 --- a/reference/mongo/mongocollection/save.xml +++ b/reference/mongo/mongocollection/save.xml @@ -4,14 +4,14 @@ MongoCollection::save - Saves an object to this collection + Saves a document to this collection &reftitle.description; public mixedMongoCollection::save - arraya + array|objecta arrayoptionsarray() @@ -30,8 +30,17 @@ - Array to save. + Array or object to save. If an object is used, it may not have protected + or private properties. + + + If the parameter does not have an _id key or + property, a new MongoId instance will be created + and assigned to it. See MongoCollection::insert for + additional information on this behavior. + + @@ -65,6 +74,11 @@ &reftitle.errors; + + Throws MongoException if the inserted document is + empty or if it contains zero-length keys. Attempting to insert an object with + protected and private properties will cause a zero-length key error. + &mongo.errors.exceptions.writeconcern; @@ -155,17 +169,6 @@ array(2) { - - &reftitle.notes; - - - This method will create an _id field in the - a array with a pregenerated ID if the field didn't - exist already - unless the argument was a reference. - - - -