C# Driver Not accepting null values

1,675 views
Skip to first unread message

Niknak

unread,
Oct 28, 2010, 10:44:14 AM10/28/10
to mongodb-user
Hi there,

Is it possible to specify a Null value as a value for a BsonElement
before it is inserted into Mongo? I'm using the latest build of the
C# driver.

....
System.NullReferenceException: Object reference not set to an instance
of an object.
at MongoDB.Bson.BsonElement.WriteTo(BsonWriter bsonWriter) in C:
\mongodb-mongo-csharp-driver-4f605c3\mongodb-mongo-csharp-
driver-4f605c3\Bson\ObjectModel\BsonElement.cs:line 282
at MongoDB.Bson.BsonDocument.SerializeDocument(BsonWriter
bsonWriter, Type nominalType, Boolean serializeIdFirst) in C:\mongodb-
mongo-csharp-driver-4f605c3\mongodb-mongo-csharp-driver-4f605c3\Bson
\ObjectModel\BsonDocument.cs:line 568
at
MongoDB.Bson.Serialization.BsonSerializer.SerializeDocument(BsonWriter
bsonWriter, Type nominalType, Object document, Boolean
serializeIdFirst) in C:\mongodb-mongo-csharp-driver-4f605c3\mongodb-
mongo-csharp-driver-4f605c3\Bson\Serialization\BsonSerializer.cs:line
207
....

All I'm doing is setting an element of a BsonDocument to "Nothing" in
VB.NET and attempting to insert it into the collection but am
receiving this error. I can provide more of the error if need be, this
is the top of the call stack, next it drops to BsonSerializer.cs line
196, and then MongoInsertMessage line 49 before it gets to my call to
the Insert method.

My main requirement for doing this is for Null DateTime elements,
rather than setting some arbitary value I would like to set a Null
value in order to show that it has not yet been set.

Nick.

Robert Stam

unread,
Oct 28, 2010, 11:10:17 AM10/28/10
to mongodb-user
Instead of setting it to Nothing (or null in C#) set it to
BsonConstants.Null.

You shouldn't be able to create an element whose value is C# null, so
I assume you must have created the element first and then overwritten
the value? Can you show me the few lines of code where you set the
value to "Nothing"? I should probably throw an ArgumentNullException
as soon as you set it to null.

Let me know if changing to BsonConstants.Null doesn't help.

Thomas Le

unread,
Sep 30, 2017, 5:16:44 PM9/30/17
to mongodb-user
Hi Niknak and Robert, 
I have the same demand, I would like to insert "null" value instead of string.Empty value for Datetime field.

Here is my code:
document.Add(dc.ColumnName.ToString(), null );

and error occurs on this line code:
MachineLogRawDataCollection.InsertOne(document);

Would you like to help me?

I would appreciate so much

Thank you,
Thomas



Wan Bachtiar

unread,
Oct 12, 2017, 3:05:26 AM10/12/17
to mongodb-user

I would like to insert “null” value instead of string.Empty value for Datetime field.

Hi Thomas,

If you’re using MongoDB C# Driver v2.3+ ,you can utilise BsonNull class to represent null.
For example:

document.Add("fieldName", BsonNull.Value ); 
collection.InsertOne(document);

If you still have further question on this, please include your MongoDB C# Driver version.

Regards,
Wan.

Tài Lê Hữu

unread,
Oct 12, 2017, 4:33:51 AM10/12/17
to mongod...@googlegroups.com
Hi Wan,
Thank you for your suggestion.
Let me follow up and let you know if i have further issue.


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/oHCfdjIo-rg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1a8a6c8c-b1a8-4340-b309-65447b77b3bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Lê Hữu.Tài
Cell No.: +84 976 049 945
Skype: taileeit
Reply all
Reply to author
Forward
0 new messages