Save a new document. Helper method, should only be used inside save().
(self, doc, force_insert, write_concern)
| 495 | return self |
| 496 | |
| 497 | def _save_create(self, doc, force_insert, write_concern): |
| 498 | """Save a new document. |
| 499 | |
| 500 | Helper method, should only be used inside save(). |
| 501 | """ |
| 502 | collection = self._get_collection() |
| 503 | with set_write_concern(collection, write_concern) as wc_collection: |
| 504 | if force_insert: |
| 505 | return wc_collection.insert_one(doc).inserted_id |
| 506 | # insert_one will provoke UniqueError alongside save does not |
| 507 | # therefore, it need to catch and call replace_one. |
| 508 | if "_id" in doc: |
| 509 | select_dict = {"_id": doc["_id"]} |
| 510 | select_dict = self._integrate_shard_key(doc, select_dict) |
| 511 | raw_object = wc_collection.find_one_and_replace(select_dict, doc) |
| 512 | if raw_object: |
| 513 | return doc["_id"] |
| 514 | |
| 515 | object_id = wc_collection.insert_one(doc).inserted_id |
| 516 | |
| 517 | return object_id |
| 518 | |
| 519 | def _get_update_doc(self): |
| 520 | """Return a dict containing all the $set and $unset operations |
no test coverage detected