[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
Mon Sep 19 19:25:52 [initandlisten] git version: nogitversion
Mon Sep 19 19:25:52 [initandlisten] build info: Linux liyong-vm.letao.com 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27 19:49:27 BST 2011 x86_64 BOOST_LIB_VERSION=1_41


Attachments: PNG File mongo.png    

 Description   

As I'm studying MongoDB I create a smplest mongodb code as the following:

//-----------Begining-----------

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++)
{
var tmp = new BsonDocument {

{ "a", i }

};

mongo["db"]["foo"].Insert(tmp);
}

///--------------End------------//

Simple enough, right? Things got weired when I try to get the count of db.foo, here's the output from my linux console:

  1. ./mongo
    MongoDB shell version: 2.0.0
    connecting to: test
    > db.foo.count() <--- Before I ran the sample code above
    0
    > use db
    switched to db db
    > db.foo.count()
    98954 <--- After
    >

//-------

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
2. Don't let the process exit until the TCP/IP buffers have all been sent

Generated at Wed Feb 07 21:36:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.