@@ -129,24 +129,23 @@ def database(self):
129
129
return self .__database
130
130
131
131
def save (self , to_save , manipulate = True , safe = False ):
132
- """Save a SON object in this collection.
132
+ """Save a document in this collection.
133
133
134
134
Raises TypeError if to_save is not an instance of dict. If `safe`
135
135
is True then the save will be checked for errors, raising
136
136
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.
138
138
139
139
:Parameters:
140
140
- `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
142
142
- `safe` (optional): check that the save succeeded?
143
143
"""
144
144
if not isinstance (to_save , types .DictType ):
145
145
raise TypeError ("cannot save object of type %s" % type (to_save ))
146
146
147
147
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 )
150
149
else :
151
150
self .update ({"_id" : to_save ["_id" ]}, to_save , True ,
152
151
manipulate , safe )
@@ -157,15 +156,15 @@ def insert(self, doc_or_docs,
157
156
"""Insert a document(s) into this collection.
158
157
159
158
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.
165
164
166
165
:Parameters:
167
166
- `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?
169
168
- `safe` (optional): check that the insert succeeded?
170
169
- `check_keys` (optional): check if keys start with '$' or
171
170
contain '.', raising `pymongo.errors.InvalidName` in either case
@@ -188,7 +187,8 @@ def insert(self, doc_or_docs,
188
187
if error :
189
188
raise OperationFailure ("insert failed: " + error ["err" ])
190
189
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
192
192
193
193
def update (self , spec , document ,
194
194
upsert = False , manipulate = False , safe = False ):
0 commit comments