@@ -140,6 +140,103 @@ can be changed to this::
140140Collection
141141----------
142142
143+ Collection.insert is removed
144+ ............................
145+
146+ Removed :meth: `pymongo.collection.Collection.insert `. Use
147+ :meth: `~pymongo.collection.Collection.insert_one ` or
148+ :meth: `~pymongo.collection.Collection.insert_many ` instead.
149+
150+ Code like this::
151+
152+ collection.insert({'doc': 1})
153+ collection.insert([{'doc': 2}, {'doc': 3}])
154+
155+ Can be changed to this::
156+
157+ collection.insert_one({'my': 'document'})
158+ collection.insert_many([{'doc': 2}, {'doc': 3}])
159+
160+ Collection.save is removed
161+ ..........................
162+
163+ Removed :meth: `pymongo.collection.Collection.save `. Applications will
164+ get better performance using :meth: `~pymongo.collection.Collection.insert_one `
165+ to insert a new document and :meth: `~pymongo.collection.Collection.update_one `
166+ to update an existing document. Code like this::
167+
168+ doc = collection.find_one({"_id": "some id"})
169+ doc["some field"] = <some value>
170+ db.collection.save(doc)
171+
172+ Can be changed to this::
173+
174+ result = collection.update_one({"_id": "some id"}, {"$set": {"some field": <some value>}})
175+
176+ If performance is not a concern and refactoring is untenable, ``save `` can be
177+ implemented like so::
178+
179+ def save(doc):
180+ if '_id' in doc:
181+ collection.replace_one({'_id': doc['_id']}, doc, upsert=True)
182+ return doc['_id']
183+ else:
184+ res = collection.insert_one(doc)
185+ return res.inserted_id
186+
187+ Collection.update is removed
188+ ............................
189+
190+ Removed :meth: `pymongo.collection.Collection.update `. Use
191+ :meth: `~pymongo.collection.Collection.update_one `
192+ to update a single document or
193+ :meth: `~pymongo.collection.Collection.update_many ` to update multiple
194+ documents. Code like this::
195+
196+ collection.update({}, {'$set': {'a': 1}})
197+ collection.update({}, {'$set': {'b': 1}}, multi=True)
198+
199+ Can be changed to this::
200+
201+ collection.update_one({}, {'$set': {'a': 1}})
202+ collection.update_many({}, {'$set': {'b': 1}})
203+
204+ Collection.remove is removed
205+ ............................
206+
207+ Removed :meth: `pymongo.collection.Collection.remove `. Use
208+ :meth: `~pymongo.collection.Collection.delete_one `
209+ to delete a single document or
210+ :meth: `~pymongo.collection.Collection.delete_many ` to delete multiple
211+ documents. Code like this::
212+
213+ collection.remove({'a': 1}, multi=False)
214+ collection.remove({'b': 1})
215+
216+ Can be changed to this::
217+
218+ collection.delete_one({'a': 1})
219+ collection.delete_many({'b': 1})
220+
221+ Collection.find_and_modify is removed
222+ .....................................
223+
224+ Removed :meth: `pymongo.collection.Collection.find_and_modify `. Use
225+ :meth: `~pymongo.collection.Collection.find_one_and_update `,
226+ :meth: `~pymongo.collection.Collection.find_one_and_replace `, or
227+ :meth: `~pymongo.collection.Collection.find_one_and_delete ` instead.
228+ Code like this::
229+
230+ updated_doc = collection.find_and_modify({'a': 1}, {'$set': {'b': 1}})
231+ replaced_doc = collection.find_and_modify({'b': 1}, {'c': 1})
232+ deleted_doc = collection.find_and_modify({'c': 1}, remove=True)
233+
234+ Can be changed to this::
235+
236+ updated_doc = collection.find_one_and_update({'a': 1}, {'$set': {'b': 1}})
237+ replaced_doc = collection.find_one_and_replace({'b': 1}, {'c': 1})
238+ deleted_doc = collection.find_one_and_delete({'c': 1})
239+
143240Collection.ensure_index is removed
144241..................................
145242
@@ -152,16 +249,16 @@ to :meth:`~pymongo.collection.Collection.create_index` or
152249:meth: `~pymongo.collection.Collection.create_indexes `. Code like this::
153250
154251 def persist(self, document):
155- my_collection .ensure_index('a', unique=True)
156- my_collection .insert_one(document)
252+ collection .ensure_index('a', unique=True)
253+ collection .insert_one(document)
157254
158255Can be changed to this::
159256
160257 def persist(self, document):
161258 if not self.created_index:
162- my_collection .create_index('a', unique=True)
259+ collection .create_index('a', unique=True)
163260 self.created_index = True
164- my_collection .insert_one(document)
261+ collection .insert_one(document)
165262
166263Collection.reindex is removed
167264.............................
0 commit comments