node.js - Index multiple MongoDB fields, make only one unique -


i've got mongodb database of metadata 300,000 photos. each has native unique id needs unique protect against duplication insertions. has time stamp.

i need run aggregate queries see how many photos have each day, have date field in format yyyy-mm-dd. not unique.

right have index on id property, (using node driver):

collection.ensureindex(    { id:1 },     { unique:true, dropdups: true },     function(err, indexname) { /* etc etc */ } ); 

the group query getting photos date takes quite long time, 1 can imagine:

collection.group(         { date: 1 },         {},         { count: 0 },         function ( curr, result ) {              result.count++;         },         function(err, grouped) { /* etc etc */ } ); 

i've read through indexing strategy, , think need index date property. don't want make unique, of course (though suppose it's fine make unique in combine unique id). should regular compound index, or can chain .ensureindex() function , specify uniqueness id field?

mongodb not have "mixed" type indexes can partially unique. on other hand why don't use _id instead of id field if possible. it's indexed , unique definition prevent inserting duplicates.

mongo can use single index in query clause - important consider when creating indexes. particular query , requirements suggest have separate unique index on id field if use _id. additionally, can create non-unique index on date field only. if run query this:

db.collection.find({"date": "01/02/2013"}).count(); 

mongo able use index answer query (covered index query) best performance can get.

note mongo won't able use compound index on (id, date) if searching date only. query has match index prefix first, i.e. if search id (id, date) index can used.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -