cKendrick
cKendrick

Reputation: 207

MongoDB Array Search in Query or client side

I am wondering what is better to do. I have a pulled back a query like this:

Array
(
[_id] => MongoId Object
    (
        [$id] => 4eeedd9545c717620a000007
    )
[field1] => ...
[field2] => ...
[field3] => ...
[field4] => ...
[field5] => ...
[field6] => ...

[votes] => Array
    (
        [whoVoted] => Array
            (
                [0] => 4f98930cb1445d0a7d000001
                [1] => 4f98959cb1445d0a7d000002
                [1] => 4f88730cb1445d0a7d000003
            )

    )

)

Which would be faster:

  1. Pull that entire array in 1 query and use in_array() to find the right id?
  2. Pull everything from the first query except the votes and then do another mongodb query to see if that id exist in the array?

Upvotes: 0

Views: 115

Answers (2)

David Odere
David Odere

Reputation: 310

It Depends on a lot of factors that I suggest you test but IMO most of the time it would be faster to just do 2 querys

Upvotes: 1

spf13
spf13

Reputation: 561

Depends on the size of the array being returned / searched.

Also different servers are doing the work, what do you mean by faster? At what scale?

Upvotes: 0

Related Questions