0

I'm currently using MongoDB and I have a collection called Product. I have a requirement in the system that asks to increment the collection version whenever any change happens to the collection (e.g. add a new product, remove, change price, etc...).

Question: Is there a recommended approach to set versions for collections in MongoDB?

I was expecting to find something like that:

db.collection.Product.setVersion("1.0.0");

and the corresponding get method:

db.collection.Product.getVersion();

I'm not sure if it makes sense. Personally, I would love to have collection metadata provided as a native implementation from MongoDB. Is there any document database that does so?

Alan Souza
  • 7,475
  • 10
  • 46
  • 68
  • MognoDB does not hold this kind of schema related information, in fact if all documents in the same collection are of the same version why do you need to set the version? If you have another collection of version 1 documents and one of version 2 why don't you just put that in the name of the collection or something? – Sammaye Jul 23 '14 at 07:03
  • That is a good question. And the answer is: I'm using an in-browser database (IndexedDB) and I need to know when to reload the client database. I'm doing this by setting the version for the Product collection. Right now this version system is a separate collection. I need the version because when the client receives a response with Version 2, and his local version is 1, I need to reinitialize IndexedDB. – Alan Souza Jul 23 '14 at 07:19

1 Answers1

0

MongoDB itself is completely "schemaless" and as such does not have any of it's own concepts of document "metadata" or the general "version management" that you seem to be looking for. As such the general implementation is all up to you, and documents store whatever you supply them with.

You could implement such a scheme, generally by wrapping methods to include such things as version management in updates. So on document creation you would do this:

db.collection.myinsert({ "field": 1, "other": 2 })

Which wraps a normal insert to do this:

db.collection.insert({ "field": 1, "other": 2, "__v": 0 })

Having that data any "updates" would need to provide a similar wrapper. So this:

db.collection.myupdate({ "field": 1 },{ "$set": { "other": 4 } })

Actually does a check for the same version as held and "increments" the version at the same time via $inc:

db.collection.update(
    { "field": 1, "__v": 0 },
    {
        "$set": { "other": 4 },
        "$inc": { "__v": 1 }
    }
)

That means the document to be modified in the database needs to match the same "version" as what is in memory in order to update. Changing the version number means subsequent updates with stale data would not succeed.

Generally though, there are several Object Document Mapper or ODM implementations available for various languages that have the sort of functionality built in. You would probably be best off looking at the Drivers section of the documentation to find something suitable for your language implementation. Also a little extra reading up on MongoDB would help as well.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Thanks for your explanation. But, in my case, I'm not looking for the version of a specific document, but for the collection as a whole. As a result, { field: 1, "__v": 0} would not fit to my scenario as it considers a specific entry. In other words, I would NOT expected to have different products with different version within the same collection. Does it make sense? – Alan Souza Jul 23 '14 at 04:29
  • @AlanSouza I sometimes miss comments. The point is largely what is said in the beginning and not all about the handling shown later. You need to implement this yourself. MongoDB is schemaless and has no "metadata". What you are calling "metadata" would actually be implemented as properties in your document and handled by your class implementations that consume it. Again, consider the available solutions before re-inventing the wheel. – Neil Lunn Jul 24 '14 at 08:13