0

I'm new with mongo

Entity:

{
    "sender": {
        "id": <unique key inside type>,
        "type": <enum value>,
    },
    "recipient": {
        "id": <unique key inside type>,
        "type": <enum value>,
    },
    ...
}

I need to create effective seach by query "find entities where sender or recipient equal to user from collection" with paging

foreach member in memberIHaveAccessTo:
    condition ||= member == recipient || member == sender

I have read some about mongo indexes. Probably my problem can be solve by storing addional field "members" which will be array contains sender and recipient and then create index on this array

  1. Is it possible to build such an index with monga?
  2. Is mongo good choise to create indexes like?
3
  • You have to make sure the index key does not grow more than 1024 byte. As you are creating indexes on the array as array size grow it might throw an exception in that case. Commented Dec 26, 2019 at 8:18
  • Just create two separate indexes and use $or in the query Commented Dec 26, 2019 at 8:29
  • @ArsenDavtyan Do I correctly understood that if I have fields A and B, create an index on A and an index on B, that is, when querying OR with two conditions for A (Condition) and B (Condition BC), the monga will somehow optimizes the query using indexes, and then combine the results so that they are unique? Commented Dec 26, 2019 at 9:05

1 Answer 1

1

Some thoughts about the issues raised in the question about querying and the application of indexes on the queried fields.

(i) The $or and two indexes:

I need to create effective search by query "find entities where sender or recipient equal to user from collection...

Your query is going to be like this:

db.test.find( { $or: [ { "sender.id": "someid" }, { "recipient.id": "someid" } ] } )

With indexes defined on "sender.id" and "recipient.id", two individual indexes, the query with the $or operator will use both the indexes.

From the docs ($or Clauses and Indexes):

When evaluating the clauses in the $or expression, MongoDB either performs a collection scan or, if all the clauses are supported by indexes, MongoDB performs index scans.

Running the query with an explain() and examining the query plan shows that indexes are used for both the conditions.


(ii) Index on members array:

Probably my problem can be solve by storing addtional field "members" which will be array contains sender and recipient and then create index on this array...

With the members array field, the query will be like this:

db.test.find( { members_array: "someid" } )

When an index is defined on members_array field, the query will use the index; the generated query plan shows the index usage. Note that an index defined on an array field is referred as Multikey Index.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.