Ricardo
Ricardo

Reputation: 8301

Create index to match a specific value in mongoDB

I want create and index to specific values to the fields producto and color

Something like:

db.ensayo.createIndex({producto:1:"myvalue",color:1:"myvalue2"});

It is posible?

Upvotes: 0

Views: 1424

Answers (2)

Yusuf Eyisan
Yusuf Eyisan

Reputation: 46

Yes, it is possible now. I think you can use this solution. You can set multiple indexes with their specific values.

db.ensayo.createIndex(
{
    "producto":1, 
    "color":1
},
{
    "unique" :true, 
    "partialFilterExpression" : {
        "producto":{ 
            "$eq": "myvalue"}, 
            "color": { "$eq": "myvalue2"}
        }
    }
);

Source: mongodb docs partialFilterExpression

Upvotes: 3

Salvador Dali
Salvador Dali

Reputation: 222939

No, it is not possible. Also it looks really strange to do this. This index will perfectly answer your query:

db.ensayo.createIndex({
   producto:1,
   color:1
})

Upvotes: 1

Related Questions