简体   繁体   English

MongoDB Java API:com.mongodb.DBCollection.Save()和com.mongodb.DBCollection.Insert()之间的区别?

[英]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? 它看起来这两个方法都将BasicDbObject保存到集合实例。这两个方法之间的区别是什么? com.mongodb.DBCollection.Insert() com.mongodb.DBCollection.Save() 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. 如果提供了_id且它已经存在,则新文档将覆盖以前存在的文档。 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. Insert可以获取对象的单个对象或ArrayList,并对它们进行“批量插入”。

If you give an existing _id to an insert it will cause a duplicate Id exception. 如果将现有的_id提供给插入,则会导致重复的Id异常。

.insert is only for insert, while .save can do insert or update. .insert仅用于插入,而.save可以插入或更新。

com.mongodb.DBCollection.Save com.mongodb.DBCollection.Save

Saves an object to this collection (does insert or update based on the object _id). 将对象保存到此集合(根据对象_id插入或更新)。

Save will either insert or update if the document has a set _id and insert will just insert. 如果文档具有set _id并且insert将仅插入,则Save将插入或更新。

You can read more about it here: http://www.mongodb.org/display/DOCS/Updating/#Updating- {{save%28%29}}inthemongoshell 你可以在这里阅读更多相关信息: http//www.mongodb.org/display/DOCS/Updating/#Updating- {{save%28%29}} inthemongoshell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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