Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a index for array in mongodb?

This is a record in mongodb.

{
  numbers: [1,2,3,4,5]
}

As title, If I create an indexing in mongo like

{
  numbers: 1
}

It will works? Or index works only on single value like number or string type?

like image 991
Vũ Anh Dũng Avatar asked Oct 25 '25 01:10

Vũ Anh Dũng


1 Answers

Yes, you can and it works:

To index a field that holds an array value, MongoDB creates an index key for each element in the array. These multikey indexes support efficient queries against array fields. Multikey indexes can be constructed over arrays that hold both scalar values 1 (e.g. strings, numbers) and nested documents.

please visit the doc: Mongodb documentation

like image 112
John Avatar answered Oct 26 '25 22:10

John