My app has a collection like this
{
sender:'1000',
receiver:'9999',
type:'text',
content:' this is content' ,
sentDate:ISODate("2011-10-12T14:54:02.069Z)
}
The collection records number > 50M.
There is a requirement to query records between sender 'X' and receiver 'Y' in short time.
First, i create index, {sender:1, receiver:1, sentDate:-1}, use query
db.msgs.find({$or:[{sender:'1000',receiver:'9999'}, { sender:'9999', receiver:'1000'}]}).sort(sentDate:-1},
but this query will not use the above index, it do scan full table. The problem is known as
Next, I change the structure of the collection like this
{
sender:'1000',
receiver:'9999',
type:'text',
both:['1000','9999']
content:' this is content' ,
sentDate:ISODate("2011-10-12T14:54:02.069Z)
}
add a new field named 'both' to store 'sender' and 'receiver' , and use query
db.msgs.find({both:{$all:['1000','9999']}).sort(sendDate:-1},.
The query do hit index, but if sender '1000' has large number of records(>100000),
the query become very slow, i saw lots page fault from mongostat.
It seems MongoDB will load all documents for indexed for {both:'1000'}, and compare whether the 'both' contents is exactly['1000','9999'].
is it right?
I have no idea how to resolve the problem. Can you give me some suggestion.
Regards.