[CSHARP-329] A very simple mongodb insert code gave wrong result Created: 23/Sep/11 Updated: 19/Oct/16 Resolved: 23/Sep/11 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | WizBe Li | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 0 |
| Labels: | C#, ConsoleApplication, Count, Insert | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 2008 R2 64bit, MS Visual C# 2010. Sample code using Windows Console project Driver is the latest official driver (1.2.0.4274) MongoDB: Mon Sep 19 19:25:52 [initandlisten] db version v2.0.0, pdfile version 4.5 |
||
| Attachments: |
|
| Description |
|
As I'm studying MongoDB I create a smplest mongodb code as the following: //----------- var mongo = MongoServer.Create("mongodb://192.168.56.101:27017"); //My mongodb server runing on my virtual box for (int i = 0; i < 100000; i++) }; mongo["db"]["foo"].Insert(tmp); ///-------------- Simple enough, right? Things got weired when I try to get the count of db.foo, here's the output from my linux console:
//------- According to the sample code, where it is 98954 should be 100000. I've repeated this code many times, results vary but none of them reached 100000. To be more sure, I run the same code using WinForm instead of WinConsole project, magically every single time the result is right. The same with Website Project. So I can be certain there's nothing wrong with my mongodb server but very likely something wrong with the driver with winconsole mode under my VS C# 2010. I also gave this code to some of my coleagues who confirmed my above findings. |
| Comments |
| Comment by Robert Stam [ 23/Sep/11 ] |
|
No problem. Glad it was something simple! |
| Comment by WizBe Li [ 23/Sep/11 ] |
|
Thanks for the quick response. I added a line to have the thread sleep for 3 seconds and problem goes away. Sorry for the troubling, thought it would be some concepts I missed about the driver. Wish a wonderful future for mongodb and all you developers. |
| Comment by Robert Stam [ 23/Sep/11 ] |
|
When you do inserts with SafeMode off the messages are queued in the outgoing TCP/IP buffers and Insert returns immediately even before the message has been sent on the network. If you let the process exit before the TCP/IP buffers have had a chance to drain out some of the messages will never have been sent, resulting in the lower count that you are seeing. There are two alternatives: 1. Use SafeMode.True, so the process waits for each Insert to complete before continuing |