[CSHARP-805] MongoDB 2.4.5 Win7 64bit fails to insert via standard csharp-driver 1.8.2 Created: 21/Aug/13  Updated: 22/Aug/13  Resolved: 21/Aug/13

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.8.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Rob Finneran Assignee: Sridhar Nanjundeswaran
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 64 bit MongoDB 2.4.5
VS 2012 .NET 4.5
C# Driver 1.8.2



 Description   

Hi

Sorry if this is a rookie mistake but I could net get inserts or updates to work. However, inserts using mongo.exe command line works just fine.

Here is my code, no errors returned, just DocumentsAffected == 0

Please Help!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;

namespace MyProjectNamespace.MongoConsoleApp
{
public class Entity
{
public ObjectId Id

{ get; set; }
public string Name { get; set; }

}

class Program
{
static void Main(string[] args)
{
string connectionString = "mongodb://localhost/test?w=1";
var client = new MongoClient(connectionString);
var entity = new Entity();
entity.Name = "test";

var server = client.GetServer();
var database = server.GetDatabase("test");

using (database.RequestStart())
{
var collection = database.GetCollection("entities");
collection.Insert(entity);
var result = database.GetLastError(); // use database instead of server
Console.WriteLine("mongodb: docs affected:

{0}", result.DocumentsAffected);
if (result.ErrorMessage != null)
{
Console.WriteLine("mongodb: {0}

", result.ErrorMessage);
}
if (result.HasLastErrorMessage)
{
Console.WriteLine("mongodb:

{0}

", result.LastErrorMessage);
}
}

Console.ReadLine();
//server.Disconnect(); disconnect did not help
}
}
}



 Comments   
Comment by Rob Finneran [ 22/Aug/13 ]

Thanks Sridhar and Craig for helping me to understand this better.

I really appreciate your help.

Comment by Craig Wilson [ 21/Aug/13 ]

Sridhar is correct that we will thrown an Exception if you have a WriteConcern other than Unacknowledged. The type of exception thrown will be a WriteConcernException. Currently, there are no timeouts for writes. However, if you have set a socket timeout and it expires, you will received a System.IO.SocketException.

Regarding the blocking, it most definitely is blocking given your code above. Not sure why you think it isn't other than it just goes really fast. You will get a WriteConcernResult back. If you didn't get an exception, then everything happened successfully and there really isn't a need to check that for inserts. However, as Sridhar said, this same result is used for Updates in which the number of documents affected will have a value.

Also, just to point this out, your connection string has w=1 and journal=true. When you explicitly pass in WriteConcern.Acknowledged, you are overriding the connection string settings.

Comment by Sridhar Nanjundeswaran [ 21/Aug/13 ]

GetLastErrorResults.DocumentAffected (n in the result of the getLastError command) is only set for updates. So if you issue an update you can then use DocumentsAffected to check the number of documents that got updated. For inserts GetLastErrorResults.DocumentAffected is always 0.
When you use WriteConcern.Acknowledged you will get an exception thrown if the insert fails (usually due to a duplicate key error).

Comment by Rob Finneran [ 21/Aug/13 ]

Hi Craig,

Thanks for all your hard work!

I was mistaken in that the record was not written. It was, so this is good news.

However, I still think there is a bug!

The WriteConcern.Acknowledged feature would not be of much use if the client program was not informed of the acknowledgement. DocumentsAffected and getLastError should tell us if the updated succeeded or not. My client code should be able to retry or determine another course of action (like to quit the program)
if writes were not 100% successful.

I changed my connection string to:

string connectionString = "mongodb://localhost/test?w=1&journal=true";

and my insert statement to:

collection.Insert(entity, WriteConcern.Acknowledged);

You would think that the C# driver should block (according to my reading of the mongodb docs, it should) and it would block until the write was successful or timed out (or some other error). I'm not sure how easy it is to test this behavior, but let us assume for the moment it does.

If it did block, the sending thread should get some result back, or at least could query for the result.
The docs imply at least at minimum the driver should throw an exception, or set the last error if the call was not successful. What error should I trap for timeouts?

Is it possible that the code is ignoring the WriteConcern.Acknowledged feature or the w=1;journel=true?
It does not appear to behave differently than the non-safe lossy mode, unless maybe it throws an error when the write was not acknowledged, maybe?

I guess I should start looking into the source code to see what is actually happening. Do you think this is a bug, a new feature request, or just a differing opinion on the right design?

Thanks!!!

Comment by Craig Wilson [ 21/Aug/13 ]

Did you actually check the database to see if your documents were there? I ran your program above and I also get documentsAffected = 0, but the documents exist in the database. I can't find any documentation for whether or not this should be 1 or 0. However, when I run this in the shell and explicitly run getLastError after I perform an insert, 0 is returned for 'n', which is the number of documents affected.

If you'd like to file a server bug, feel free, but this is not a bug in the .NET driver as we just report what the server tells us.

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