Grofit
Grofit

Reputation: 18445

C# MongoDb querying and using underlying JSON

Hopefully this is a simple one, but not sure if it will be as information online seems to indicate that you are unable to query without using a strongly typed model.

To give some background to the scenario, I have a client side javascript application, which creates and stores models locally (local storage). However there is a web service (ASP MVC) which can be sent models via authorised users and it will validate it and persist it.

Now the problem is that as the client side application is updated the models tend to change, think of it like database migration scripts, but with models. However they all have versions and migration scripts so the system will allow a saved (client side) older model to be loaded and then updated to the latest version and re-saved.

So the client side deals with differing version models well, and im sure your all wondering where the mongodb/c# question is, so here it comes.

On the server side the validation criteria is always against the latest version of the model at that given time. So if the latest client side model version is 2.3 then the server side will be the same, and will validate it correctly.

So at this point lets say I save my client side model @ version 3.1, the server validates version 3.1 and persists it to MongoDB. Then I go away for a year, and when I come back the client side version is 5.1, and I have formatted my computer so I no longer have the local data, so I go to download it, however the 5.1 model is quite different to version 3.1. The server is not equipped to migrate the data, HOWEVER we know that it was valid when it was entered into the database, so there is no reason why the JSON in the database cant be sent to the client and then it will be auto migrated when its loaded into memory (and then re-saved on the client). Now due to the servers model representing the latest version (5.1) I imagine it would either exception or just fill in what it can and default the rest of the variables in the model. (The client can update the server model so the server model can be updated via the client, incase you were worrying about the data always being out of date.)

So as all queries are against 2 fields which I know will never change (AccountId, PersistedId), I dont see any reason why I couldn't query MongoDB objects without a strongly typed object to get back the raw BSON Document, then call ToJson() on that BSON document and send back to the client. So is there any way to read data without having to use a strong type? So I can validate the writes and use a strongly typed model there, but on pulling the data out I just want to query the raw BSON?

Hopefully this makes sense even if it is long winded, just wanted to give context to avoid the "why can't you use a strongly typed model?" etc.

Upvotes: 0

Views: 790

Answers (1)

Craig Wilson
Craig Wilson

Reputation: 12624

So, you can query using raw BsonDocuments. These are untyped, but can be created using typed models. Maybe this is a compromise?

var queryDocument = Query<Person>.Eq(p => p.Name, "Jack");
// { "name" : "Jack" }

Otherwise, if you are intent on having your versions work this way, there is no magical bullet to handle schema migration. It is a problem across all database stacks. We have some documentation written for dealing with it in the serialization tutorial.

Upvotes: 1

Related Questions