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

IMongoCollection<BsonValue> causes InsertOne to fail to insert "_id" member

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • 2.4.4
    • BSON
    • .Net Framework 4.5.2 on Windows 10

    Description

      If I use IMongoCollection<BsonValue> rather than IMongoCollection<BsonDocument>, and use the InsertOne method to add a document which does not specify its own "_id" value, then InsertOne fails to insert the auto-generated "_id" member into the document object.

                      MongoClient myClient = new MongoDB.Driver.MongoClient(ConnectionConf);
                      var myDB = myClient.GetDatabase(DBName);
       
                      bool avoidBug = false;
       
                      if (avoidBug)
                      {
                          IMongoCollection<BsonDocument> myCollectionGood = myDB.GetCollection<BsonDocument>(CollectionName);
                          // InsertOne modifies doc as expected: it adds an "_id" member
                          myCollectionGood.InsertOne(doc);
                      }
                      else
                      {
                          IMongoCollection<BsonValue> myCollectionBad = myDB.GetCollection<BsonValue>(CollectionName);
                          // InsertOne fails to modify doc: it does not add the "_id" member
                          myCollectionBad.InsertOne(doc);
                      }
      

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            Max-IandF Max Barraclough [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: