My application is, two person are chatting, I want to fetch the past
chat, in less number of operations, in terms of time
So, I tried to use index on (fromId : 1,toId : 1,sentOn : -1)
let the chat be:
b...@d.com : '1' (to
s...@d.com) at time1
s...@d.com : '2' (to
d...@d.com) at time2
b...@d.com : '3' (to
s...@d.com) at time3
s...@d.com : '4' (to
d...@d.com) at time4
time1<time2<time3<time4
and there are other chats of
b...@d.com and
s...@d.com with other person in
the same table, so I decided to go for index (fromId : 1,toId :
1,sentOn : -1) so that I can make benefit of the index to optimize the
time
so, I just want to fetch the last 2 message i.e.
b...@d.com : '3' (to
s...@d.com) at time3
s...@d.com : '4' (to
d...@d.com) at time4
so what can be more efficient way of doing...
-------------------------------------------------------
Your saying calling two cursors with limit() is quicker than calling
one
cursor with $or?
As per I checked in my DB, when I did $or as told by you and called
explain() function which gave me following result
{
"cursor" : "BasicCursor",
"nscanned" : 7594,
"nscannedObjects" : 7594,
"n" : 100,
"scanAndOrder" : true,
"millis" : 32,
"indexBounds" : {
}
}
but, in my case using two cursors, nscanned Objects are just (100+100)
then have to do sorting to get the latest 100
Kindly, explain if I am wrong anywhere.......
On Sep 13, 10:45 pm, Sam Millman <
sam.mill...@gmail.com> wrote:
> "So, in both case I don't require to call sort() function, which is a
> costly affair, now I need to do the sort,"
>
> I just guessed your sort. If you want the last two then your index will work
> fine without the sort.
>
> "it will take a lot more time, because query parsing plan is not as
> powerful in mongo as in mysql, is scans the entries, do sorting and
> then put limit()"
>
> Your saying calling two cursors with limit() is quicker than calling one
> cursor with $or?
>
> What you gotta remember is that MongoDB's querying system is a lot slimmer
> than SQLs which requires tonnes fo stuff that MongoDB just does not
> implement.
>
> Because of that it doesn't make sense
>
> "without using the sort, so total no of scanned elements was
> just 2 (and it was ensured that these are latest messages from
> ["
b...@d.com" to "
s...@d.com"])
> same with the case of cur"
>
> But I do get what you are trying to do, which is to two get the latest two
> messages from two specific people.
>
> As such the only method left for you is client side sorting.
>