Skip to content

Commit 0ac83e4

Browse files
author
Mike Dirolf
committed
API change: both insert and save return inserted _id(s)
1 parent 819778a commit 0ac83e4

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

gridfs/grid_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class directly - instead see the `gridfs.GridFS.open` method.
107107
file_spec["length"] = 0
108108
file_spec["uploadDate"] = datetime.datetime.utcnow()
109109
file_spec.setdefault("chunkSize", 256000)
110-
self.__id = self.__collection.files.insert(file_spec)["_id"]
110+
self.__id = self.__collection.files.insert(file_spec)
111111

112112
# we use repr(self.__id) here because we need it to be string and
113113
# filename gets tricky with renaming. this is a hack.

pymongo/collection.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,23 @@ def database(self):
129129
return self.__database
130130

131131
def save(self, to_save, manipulate=True, safe=False):
132-
"""Save a SON object in this collection.
132+
"""Save a document in this collection.
133133
134134
Raises TypeError if to_save is not an instance of dict. If `safe`
135135
is True then the save will be checked for errors, raising
136136
OperationFailure if one occurred. Checking for safety requires an extra
137-
round-trip to the database.
137+
round-trip to the database. Returns the _id of the saved document.
138138
139139
:Parameters:
140140
- `to_save`: the SON object to be saved
141-
- `manipulate` (optional): manipulate the son object before saving it
141+
- `manipulate` (optional): manipulate the SON object before saving it
142142
- `safe` (optional): check that the save succeeded?
143143
"""
144144
if not isinstance(to_save, types.DictType):
145145
raise TypeError("cannot save object of type %s" % type(to_save))
146146

147147
if "_id" not in to_save:
148-
result = self.insert(to_save, manipulate, safe)
149-
return result.get("_id", None)
148+
return self.insert(to_save, manipulate, safe)
150149
else:
151150
self.update({"_id": to_save["_id"]}, to_save, True,
152151
manipulate, safe)
@@ -157,15 +156,15 @@ def insert(self, doc_or_docs,
157156
"""Insert a document(s) into this collection.
158157
159158
If manipulate is set the document(s) are manipulated using any
160-
SONManipulators that have been added to this database. Returns the
161-
inserted object or a list of inserted objects. If `safe` is True then
162-
the insert will be checked for errors, raising OperationFailure if one
163-
occurred. Checking for safety requires an extra round-trip to the
164-
database.
159+
SONManipulators that have been added to this database. Returns the _id
160+
of the inserted document or a list of _ids of the inserted documents.
161+
If `safe` is True then the insert will be checked for errors, raising
162+
OperationFailure if one occurred. Checking for safety requires an extra
163+
round-trip to the database.
165164
166165
:Parameters:
167166
- `doc_or_docs`: a SON object or list of SON objects to be inserted
168-
- `manipulate` (optional): monipulate the objects before inserting?
167+
- `manipulate` (optional): monipulate the documents before inserting?
169168
- `safe` (optional): check that the insert succeeded?
170169
- `check_keys` (optional): check if keys start with '$' or
171170
contain '.', raising `pymongo.errors.InvalidName` in either case
@@ -188,7 +187,8 @@ def insert(self, doc_or_docs,
188187
if error:
189188
raise OperationFailure("insert failed: " + error["err"])
190189

191-
return len(docs) == 1 and docs[0] or docs
190+
ids = [doc.get("_id", None) for doc in docs]
191+
return len(ids) == 1 and ids[0] or ids
192192

193193
def update(self, spec, document,
194194
upsert=False, manipulate=False, safe=False):

test/test_collection.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,11 @@ def test_insert_find_one(self):
285285
db.test.remove({})
286286
self.assertEqual(db.test.find().count(), 0)
287287
doc = {"hello": u"world"}
288-
db.test.insert(doc)
288+
id = db.test.insert(doc)
289289
self.assertEqual(db.test.find().count(), 1)
290290
self.assertEqual(doc, db.test.find_one())
291+
self.assertEqual(doc["_id"], id)
292+
self.assert_(isinstance(id, ObjectId))
291293

292294
def remove_insert_find_one(dict):
293295
db.test.remove({})
@@ -396,11 +398,21 @@ def test_insert_multiple(self):
396398
doc1 = {"hello": u"world"}
397399
doc2 = {"hello": u"mike"}
398400
self.assertEqual(db.test.find().count(), 0)
399-
db.test.insert([doc1, doc2])
401+
ids = db.test.insert([doc1, doc2])
400402
self.assertEqual(db.test.find().count(), 2)
401403
self.assertEqual(doc1, db.test.find_one({"hello": u"world"}))
402404
self.assertEqual(doc2, db.test.find_one({"hello": u"mike"}))
403405

406+
self.assertEqual(2, len(ids))
407+
self.assertEqual(doc1["_id"], ids[0])
408+
self.assertEqual(doc2["_id"], ids[1])
409+
410+
def test_save(self):
411+
self.db.drop_collection("test")
412+
id = self.db.test.save({"hello": "world"})
413+
self.assertEqual(self.db.test.find_one()["_id"], id)
414+
self.assert_(isinstance(id, ObjectId))
415+
404416
def test_unique_index(self):
405417
db = self.db
406418

0 commit comments

Comments
 (0)