mardi 2 décembre 2014

Mongodb compound index on two fields where one field is sparse


I have a collection of documents that give stats relating to videos. I have a unique compound index on videoId, date (there is at most one document per video per day).


I want to be able to; in a single query pull the most recent document for all videos. The only way I can think of doing this is to programatically maintain a boolean 'latest' flag in these documents. This won't be too difficult for my use-case. I'm just wondering about how best to index this.


I could just put a sparse index on 'latest' which should be useful in 2.6 with index intersection. However I'd ideally also like to use the index to force consistency i.e. there can be only one doc per video with 'latest' attribute. I can't do a unique compound index on latest, videoId without setting latest to something arbitrary/unique on documents that are not the latest...


Can anyone suggest a cleaner solution?





Aucun commentaire:

Enregistrer un commentaire