c# driver BSON null when child array is empty

1,864 views
Skip to first unread message

Alex

unread,
Dec 7, 2010, 10:28:25 AM12/7/10
to mongodb-user
Using the latest .9 driver. Not sure if this is expected behavior or
whether there is something wrong in the driver. However, if I create
an object that contains IList<T> and I create the list object (new
List<T>()) but do not populate it with anything, the default
serializer creates an empty array for that property when the BSON
Document is created. When the generated BSON document is sent to the
database during an insert, the insert completes correctly but the
server returns a BsonNull error when I execute GetLastError(). I do
not get an error if I populate the list with at least 1 element.

Alex

unread,
Dec 7, 2010, 10:44:00 AM12/7/10
to mongodb-user
Also, just so I understand the atomic operation issue, isn't an insert
a single atomic operation? The document still gets saved even though
an exception is thrown by the server. I thought that if there is an
exception within a single document operation that the document
operation would not complete.

Thanks,
Alex

Robert Stam

unread,
Dec 7, 2010, 10:59:47 AM12/7/10
to mongodb-user
Can you provide a stack trace? You can remove the part of the stack
trace that is in your application if you wish.

Robert Stam

unread,
Dec 7, 2010, 11:24:09 AM12/7/10
to mongodb-user
Here's what I think is happening:

1. The insert is successful
2. The server is returning "{ 'err' : null, 'n' : 0, 'ok' : 1 }" for
GetLastError
3. The GetLastErrorResult.LastErrorMessage property should return C#
null instead of "BsonNull" in this case

In short: everything is fine, there is no error (other than the
bizarre value for LastErrorMessage).

Also, the insert operation and the GetLastError operation are two
separate operations, so it is theoretically possible for the insert to
succeed and the GetLastError to fail (for example, you lost your
network connection just between the two operations).

Note: I get the exact same results whether I insert an empty list or a
populated list, so in that respect my results are different from
yours.

I've created a JIRA ticket to fix the LastErrorMessage property:

http://jira.mongodb.org/browse/CSHARP-130

Alex

unread,
Dec 7, 2010, 11:30:23 AM12/7/10
to mongodb-user
Unfortunately, stack trace does not provide anything since no
exceptions are thrown except from the dbserver. I think the problem
might lie in the EnumerableSerializer<T>. If you look at lines
85-95, they are as follows:

if (value == null) {
bsonWriter.WriteNull();
} else {
bsonWriter.WriteStartArray();
int index = 0;
foreach (var element in (IEnumerable<T>) value) {
bsonWriter.WriteName(index.ToString());
BsonSerializer.Serialize(bsonWriter, typeof(T),
element);
index++;
}
bsonWriter.WriteEndArray();
}

I think the problem is that the List is not null but it is empty and
the dbserver does not seem to like the empty array being sent to it
(even though it does save the record).

Alex

On Dec 7, 10:59 am, Robert Stam <rstam10...@gmail.com> wrote:

Robert Stam

unread,
Dec 7, 2010, 11:34:54 AM12/7/10
to mongodb-user
We were probably both typing at the same time. Does my previous reply
answer your question?

Alex

unread,
Dec 7, 2010, 12:03:14 PM12/7/10
to mongodb-user
Yup, I was just about to add that the errormessage did not seem like
an error message at all because ErrorMessage was actually NULL and ok
was set to 1 while LastErrorMessage was set to BsonNull. In my code,
because I was doing a check of LastErrorMessage == null, I was
stumbling on the issue. So everything was working correctly except
the LastErrorMessage value.

Thanks for the quick assistance.

Alex
Reply all
Reply to author
Forward
0 new messages