Home > OS >  How to index a MongoDB field that holds an array of strings?
How to index a MongoDB field that holds an array of strings?

Time:09-23

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.

CodePudding user response:

You can create an index like this:

db.col.createIndex({ 'field_a': 1 })
  • Related