This is my document structure :
db.like
{
_id: objectid, /* not null */
id_from: int64, /* not null */
id_to: int64, /* not null */
date: datetime, /* default NOW not null */
}
db.like.createIndex( {"id_to": 1, "id_from": 1}, {unique: true} );
db.like.createIndex( {"id_to": 1, "date": -1}, {unique: false} );
I load document only in one of these ways:
db.like.find({$and: [{id_to:xxx}, {id_from:yyyy}]})
or
db.like.find({id_to:xxx}).sort({date:-1});
and later i shard the collection like this :
sh.shardCollection( "like", {"id_to": 1, "id_from": 1}, unique: true );
As you see i don't use at all the index on "_id"
. I m a little worry to have an index on "_id"
that seam to be useless. Is their a way to optimize my schema or better to leave it like this ?
NOTE: the solution must work with sharding, so the solution given by clcto seam to be bad for this! it's solution was to declare _id as a document like :
{
_id : {
to : int64,
from : int64
},
date : datetime
}
but i m quite sure that with such declaration query like
db.like.find({id_to:xxx}).sort({date:-1});
will be done on all shards