Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-329

A very simple mongodb insert code gave wrong result

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.2
    • Component/s: None
    • Environment:

      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.

        1. mongo.png
          12 kB
          WizBe Li

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            wizbe WizBe Li
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: