2014년 12월 10일 수요일

variable in mongodb

i want to store somewhere in mongodb database a variable named schema_version. how to do ?

the purpose is that i want that every client before to do anything on a collection check if their own schema_version = the database schema_version. off course i can create a collection with just schema_version inside, but it's little sad that for every query on the database i will need to always do another query on the schema_version collection.



Create a one document collection and store the schema version in there, as you suggested. I don't understand why you say it's sad to store something this way in the database. If you store something in the database, what other option exists to find the information again besides to query for it?



I mean, for every update/insert/delete/read i will need first to do one query on this schema collection :(

i would prefere to declare a "variable" in javascript inside mongodb and be able to do somethink like this

update 
...
Where
  var_schema_version <= 245 and
  _ID=xxxx

or

select
  ...
  var_schema_version
from
...



There's no concept of global variables that you can define for the server like that. If there were, we'd probably store it in a collection anyway, and you'd still be doing the super-fast one-document lookup that makes you sad :D. Moreover, the extra cost of using a where clause and executing server-side javascript could be more than just doing an extra lookup by _id.

Would you mind describing the requirements around synchronizing client and server schema versions in more detail? We might be able to offer some other helpful strategies.



>> Would you mind describing the requirements around synchronizing client and server schema versions in more detail? We might be able to offer some other helpful strategies.

ok, so we have one database, one collection, with a schema. ex
Mycollection: {
  _id: int64,
  datta: text // yes spelling mistage
}
say this schema version is 1

after we have many clients (build from differents developpers) that can query/update this collection.
what i want is that if today all clients are build on the schema version 1. if tomorow we update the
schema to:

Mycollection: {
  _id: int64,
  date: datetime,
  data: text
}
schema version is now 2

then we must forbid any clients who forget to update their internal schema definition to version 2 to do anything
with mycollection !

but i don't know the best way to do it :(



So the clients are third party code interacting directly with the database? I think it's wise to abstract and secure the database from unknown third party code with some kind of an API layer, from which you can enforce schema versions. You should be able to enforce schema versions more conveniently that way by making the check occur as part of authenticating or establishing a session with the API layer. How often do you think the schema version will change?



>>So the clients are third party code interacting directly with the database?

somethink like this yes


>>I think it's wise to abstract and secure the database from unknown third party code with some kind of an API layer, from which you can enforce schema versions. You should be able to enforce schema versions more conveniently that way by making the check occur as >>part of authenticating or establishing a session with the API layer. 

but it's much much more complicate to do :( much much more :( it's not an easy job at all to build this api :(
actually we choose to create a dll with all the functions to work on the database. each client use this dll to update/read the data via "interface" object. The probleme is when a client "forget" to update his dll :(


>>How often do you think the schema version will change?

don't really know, i guess one time a month



Regardless of the difficulty, I think it is by far the more maintainable and more secure option. At the very least, you could write a lightweight layer that verifies schema version and establishes some kind of a session for each client. If you update the schema, you force the clients to refresh their sessions. This way you only need to verify the version once per session.



yes, but it's not easy to do :( we don't have trigger anything in mongodb, it's even schema free ... but at the end we are forced to deploy an "api" framework in the middle between client and database :(
i want to avoid the api framework, that again not easy to do (especially about monitoring tail, etc..)

here it's simple, a global variable "schema_version" that we can add/retrieve in all the queries ! i think easy to do and will be a very powerfull feature !


댓글 없음:

댓글 쓰기