0

In my collection, documents will contain a field that holds an array like so:

{
  field_a: ["banna", "orange", "kiwi"]
}

How to index this collection based on this field?

The queries will be something like:

Find all documents where field_a is a subset of a given array.

3
  • Are you using Mongoose? Do you have a schema? Commented Sep 22, 2022 at 14:14
  • no mongoose. no schema. Commented Sep 22, 2022 at 14:22
  • 1
    Multikeys indexes exists in mongo but i'm not sure it's useful in your use case. It just splits the array so i'm not sure it's useful for mongo to determine if it's a subset. The thing you can do is to test it =) You can create a multikeys the same way you would create an index, just test it ! =) Commented Sep 22, 2022 at 14:33

1 Answer 1

1

You can create an index like this:

db.col.createIndex({ 'field_a': 1 })
Sign up to request clarification or add additional context in comments.

2 Comments

Will this index work on the query that I described?
You want to send send an input array to a query, and field_a should have all the items of that array, right? If that is the case, then yes.

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.