[SERVER-13536] WriteResult only mentions the last insert if inserts happen in a for loop Created: 10/Apr/14  Updated: 10/Dec/14  Resolved: 10/Apr/14

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.6.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: William Cross Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

> for( i = 1 ; i <= 10 ; i++ ) { for( j = 1; j <= 10 ; j++ ) { for( k = 1 ; k <= 10 ; k++ ) { db.foo.insert( { a : i , b : j , c : k } ) } } }
WriteResult({ "nInserted" : 1 })

Expected result: 1,000 documents are inserted, and WriteResult(

{ "nInserted" : 1000 }

)

Actual result: 1,000 documents are inserted (good) but WriteResult(

{ "nInserted" : 1 }

) is displayed, implying that we've only added one document to the collection.

Participants:

 Description   

If I have a for loop that inserts documents in the shell, only the last insert will give a WriteResult.

This is confusing because LOTS of documents may have been inserted, yet all we see is WriteResult: 1.



 Comments   
Comment by William Cross [ 10/Apr/14 ]

milkie
Yes, thank you for the reminder!

I need to internalize the new stuff so that I remember to use it in the training guides & videos.

Comment by Eric Milkie [ 10/Apr/14 ]

Note that with the new bulk insert ability of the shell in 2.6, you could be rewriting that for-loop; the result could then be nInserted=1000 after you call execute().

Comment by William Cross [ 10/Apr/14 ]

Wacky.

OK, thanks for the lesson on javascript consoles. Bug withdrawn.

Comment by J Rassi [ 10/Apr/14 ]

This is a side effect of the fact that the MongoDB interactive shell presents the user with a standard JavaScript interactive console. When executing a line of JavaScript at the MongoDB interactive shell, the result of the evaluation of the last executed statement is printed to the console. The Collection.insert() function returns a WriteResult corresponding to the given insert, so you're simply seeing the return value of the last call to Collection.insert().

Try running the following at the interactive shell (or your favorite browser's JavaScript console, for that matter); it exhibits the same behavior.

> for (i=0; i<8; i++) { Math.pow(2, i); }
128

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