简体   繁体   中英

MongoDB Java API: Difference between com.mongodb.DBCollection.Save() and com.mongodb.DBCollection.Insert()?

It looks these two methods both save a BasicDbObject to a collection instance.What is the difference between these two methods? com.mongodb.DBCollection.Insert() com.mongodb.DBCollection.Save()

Save can only save one object at a time. If _id is provided and it already exists, the new document will overwrite the previously existing one. If it doesn't exist, it will do an insert.

Insert can take a single object or an ArrayList of objects and do a "bulk insert" of them.

If you give an existing _id to an insert it will cause a duplicate Id exception.

.insert is only for insert, while .save can do insert or update.

com.mongodb.DBCollection.Save

Saves an object to this collection (does insert or update based on the object _id).

Save will either insert or update if the document has a set _id and insert will just insert.

You can read more about it here: http://www.mongodb.org/display/DOCS/Updating/#Updating- {{save%28%29}}inthemongoshell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM