[JAVA-1270] Class WriteResult Method getN() always return 0 Created: 10/Jun/14  Updated: 23/Jun/15  Resolved: 23/Jun/15

Status: Closed
Project: Java Driver
Component/s: Documentation
Affects Version/s: None
Fix Version/s: None

Type: Improvement
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

my environment :
mongodb used : mongodb-win32-x86_64-2008plus-2.6.1
java environmen t: mongo-java-driver-2.12.2.jar
springMVC 3.2

Location: http://www.mongodb.org/about/support/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Screen Resolution: 1600 x 900
repo: mongodb-www-about
source: support



 Description   

DBObject user = new BasicDBObject();
user.put("name", "hoojo");
user.put("age", 23);
WriteResult wr = dbCollection.insert(user);// dbCollection.save(user);

method wr.getN() return 0 both with insert and save,
but the data save success.

Reporter: luoch
E-mail: luoch668@163.com



 Comments   
Comment by Jeffrey Yemin [ 23/Jun/15 ]

In the new CRUD API introduced in 3.0, the insertOne/insertMany methods on MongoCollection return void. The number of documents inserted is either one for insertOne, or n for insertMany, where n equals the number of documents passed to insertMany. There is no need for these methods to return n, since it's value is implied by the method name and/or their arguments.

Comment by Jeffrey Yemin [ 16/Dec/14 ]

Only by using the bulk write support introduced in 2.12.0. See http://api.mongodb.org/java/current/com/mongodb/BulkWriteOperation.html for details.

Comment by Tyler Hale [ 16/Dec/14 ]

Is there any other way to retrieve the number of documents that were successfully inserted?

Comment by sjkyspa [ 09/Jul/14 ]

getUpsertedId()
Gets the _id value of an upserted document that resulted from this write.
this is from the official document, but it seems that the upserted id is null, this will cause null exception. Is this by design?

Comment by Jeffrey Yemin [ 10/Jun/14 ]

It's by design. The getlasterror command has always return n : 0 for inserts, and the driver is just mirroring that response. 'n' in this case represents the number of documents matched by the query (for update and remove), and there is no query for insert, so it's always 0.

Generated at Thu Feb 08 08:54:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.