1

I am trying to perform an update using strongly-typed objects. For example,

public void setAppointmentPrefs(string UserName, IEnumerable<AppointmentInfo> info)
{
    var query = new QueryDocument {{ "ProviderId", UserName}};
    var update = Update.Set("Prefs",prefs);    // prefs.toList() gives same error
    // providerprefs initialized in constructor
    providerprefs.Update(query, update);
}

I receive a compiler error saying:Error 14 The best overloaded method match for 'MongoDB.Driver.Builders.Update.Set(string, MongoDB.Bson.BsonValue)' has some invalid arguments

Obviously the Mongo driver will not let me update based on my own object (whether as IEnumerable or prefs.toList()), which seems a contrast from the way it permits me to insert or query with custom objects. Surely I am missing something obvious that would permit me to avoid deserializing, weakly typing then creating a generic BsonDocument!! TIA.

2 Answers 2

2

You can do an Update based on your own types! Have you tried using the typed Query and Update builders?

Try something like this:

var query = Query<AppointmentInfo>.EQ(i => i.ProviderId, userName);
var update = Update<AppointmentInfo>.Set(i => i.Prefs, info.Prefs);

Not sure I got the types and everything write from your partial code, but that should give you the general idea.

Let me know if you have any further questions.

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

Comments

1

I know this has been answered but I for one don't fully understand Roberts answer.

All I did is call the "ToBsonDocument()" method for it to except the object as a parameter

So:

customObject.ToBsonDocument()

If you have an array of objects inside a document:

var query = Query.EQ("_id", ObjectId.Parse(id.ToString()));
var update = Update.Push("ArrayOfObjects", customObject.ToBsonDocument());
collection.Update(query, update);

Comments

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.