6

I am trying to update an existing Mongo record, but am getting an "Additional information: Element name 'ID' is not valid'." error

I have a a BsonDocument "document" containing data that I retrieve from another source that looks like this:

{ "ID" : "ABCecdcf9851efbf0ef66953", ListingKey : "234534345345", "Created" : ISODate("2017-08-04T00:31:23.357Z"), "Modified" : ISODate("2017-08-04T00:31:23.358Z"), "Field1" : 1, "Field2" : "0.09", "Field3" : "1.10", "Field4" : "1", "Field5" : "1" }

Here is the C# code that I have written:

var collection = db.GetCollection<BsonDocument>("MyCollection");                              

//Hard coded for testing
var filter = Builders<BsonDocument>.Filter.Eq("ListingKey", "234534345345");

collection.UpdateOne(filter, document);

Is this related to the BsonDocument that I am trying to use to update? I found this documentation, which causes me to think that this is the cause. If so, is there a way to do an update with the format I have been provided?

https://docs.mongodb.com/getting-started/csharp/update/

I had a process working where it would delete the document and then add a new document, but for efficiency's sake I need this to update. Ideally it will only update the fields that are present in the BsonDocument and keep the existing fields in the Mongo document as is.

6
  • What is the actual content of document? If it actually looks exactly like you list above then ISODate is not a valid argument as there is no such object constructor in C#. You probably have a JSON string or just a "string" for that property. Hence the error. Commented Aug 4, 2017 at 0:52
  • @NeilLunn I assume that it's a json string. It's failing on the first element (ID). I'll update the question with better data. This field is not an ObjectId or anything special in Mongo. Commented Aug 4, 2017 at 0:55
  • Well I "assume" as much as well. But no-one can actually be sure unless you present the "full context" of what you are trying to do here. Which is what I asked for and why I asked it. So please Edit your question with the required detail that is asked for. Commented Aug 4, 2017 at 0:59
  • @NeilLunn. Sorry, I see how it's worded poorly. The top block of code it what's in the document variable. Commented Aug 4, 2017 at 1:00
  • { "ID" : "ABCecdcf9851efbf0ef66953", ListingKey : "234534345345", "Created" : ISODate("2017-08-04T00:31:23.357Z"), "Modified" : ISODate("2017-08-04T00:31:23.358Z"), "Field1" : 1, "Field2" : "0.09", "Field3" : "1.10", "Field4" : "1", "Field5" : "1" } Commented Aug 4, 2017 at 1:01

1 Answer 1

11

My problem was because I did not have the correct value when trying to update. My code works with this:

var collection = db.GetCollection<BsonDocument>("MyCollection");                              

//Hard coded for testing
var filter = Builders<BsonDocument>.Filter.Eq("ListingKey", "234534345345");

var update = Builders<BsonDocument>.Update.Set("Created", DateTime.UtcNow);
foreach (BsonElement item in document)
{
    update = update.Set(item.Name, item.Value);
}
var result = collection.UpdateOne(filter, update);

I had to convert my string into an update BsonDocument.

Sign up to request clarification or add additional context in comments.

1 Comment

I had a case with class (bank-account) that could be inserted if it was a new "account" after converting it with ToBsonDocument() (then inserted with collection.InsertOne() ), while if it was not a new "account" it could be updated just like you show with Filter and Update, and I didn't need the foreach loop (and I didn't understand what <document> was). Thanks @ferensilver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.