6

I know that a compound index is defined like this:

db.products.ensureIndex( { "item": 1, "stock": 1 } )

and a hashed a simple index like this:

db.active.ensureIndex( { item: "hashed" } )

question is how to achieve both?

Naman
  • 27,789
  • 26
  • 218
  • 353
ramon_salla
  • 1,587
  • 1
  • 17
  • 35
  • 1
    Apparently, 7 yrs down the line, you can now [do this with compatibility version 4.4](https://stackoverflow.com/a/65844259/1746118). – Naman Feb 01 '21 at 07:09

3 Answers3

5

According to the hashed index documentaion You can't!

MongoDB supports hashed indexes of any single field. The hashing function collapses sub-documents and computes the hash for the entire value, but does not support multi-key (i.e. arrays) indexes.

You may not create compound indexes that have hashed index fields

PS: The above is valid for versions 2.4 and 2.6 (which is the latest at the moment)

PS2: According to @naman's answer it is now possible in version 4.4

xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • Multikey index is not the same as Compound index. In the example, item and stock are not arrays. – ramon_salla Feb 01 '14 at 19:30
  • 2
    I know, I didn't say the oposite. At the end it says that **you may not create compound indexes that have hashed index fields**, and that you can only create a compound index of a single field, which answers what you are asking. If you absolutely need the hashed index of those two fields, make them a single field as a subdocument, which, according to the doc, works. – xlembouras Feb 01 '14 at 19:35
  • Well, my intention is to do what you say at the end, compose the two fields and then calculate the hash, not to hash a single field and then compose it (as it is clearly not allowed as documentation says). I just supposed there was a way to hash the automatically composed field. But as you say, I will need to create a new field and hash it. – ramon_salla Feb 01 '14 at 20:12
  • You can vote on this feature being included in future versions at: https://jira.mongodb.org/browse/SERVER-10220 – Nic Cottrell Dec 26 '14 at 08:33
  • Apparently, 7 yrs down the line, you can now [do this with compatibility version 4.4](https://stackoverflow.com/a/65844259/1746118). – Naman Feb 01 '21 at 07:08
  • Nice! It was about time. – xlembouras Feb 01 '21 at 07:22
2

If you want to achieve a compound hashed index, it's feasible with version 4.4 and above. From the documentation, you can now create it as:

db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )
db.collection.createIndex( { "fieldA" : 1, "fieldB" : "hashed", "fieldC" : -1 } )

for the particular example in question

db.products.ensureIndex( { "item": "hashed", "stock": 1 } )
Naman
  • 27,789
  • 26
  • 218
  • 353
0

MongoDB 4.4 support compound index with single hashed which can be created like

db.collection.createIndex( { "colA" : 1, "fieldB" : "hashed" } )

Note: Make sure featureCompatibilityVersion set to 4.4 so that you can create compound hashed index.

db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )

ROHIT KHURANA
  • 903
  • 7
  • 13
  • thanks for the contribution, the bounty here is to [draw attention to this answer](https://stackoverflow.com/a/65844259/1746118), the answer you made would end up being a duplicate. hence, would suggest closing it in favor of the other answer. – Naman Jan 29 '21 at 11:26