question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compound Unique indexes

See original GitHub issue

Feature Request:

A cool to have compound unique indexes in a schema for instance.

var Schema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    product_slug : {
        type: String,
        index: true
    },
    store_slug: {
        type: String
        index: true
    }
});


Schema.unique({store_slug : {product_slug : 1}});

So product_slug must be unique among all objects with the same store_slug.

This definitely could be achieved with a mongoose plugin however I think this could give mongoose a lot of power right out of the box and could be included.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Mar 8, 2016

Schema.index({ store_slug: 1, product_slug: 1 }, { unique: true }); https://docs.mongodb.org/manual/tutorial/create-a-unique-index/#unique-compound-index

0reactions
florianwalther-privatecommented, Sep 23, 2022

@vkarpov15 Thank you for the clarifiation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unique Indexes — MongoDB Manual
A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB...
Read more >
How to create a composite unique index (not as a primary key ...
1. Open the table in Design View. · 2. Click Indexes button. · 3. Enter the first column for the index. · 4....
Read more >
MongoDB Unique Index
When a unique index contains more than one field, it is called a unique compound index. A unique compound index ensures the uniqueness...
Read more >
Unique and non-unique indexes - IBM
Unique indexes are indexes that help maintain data integrity by ensuring that no rows of data in a table have identical key values....
Read more >
mongodb compound index unique if A and B both are same
mongodb compound index unique if A and B both are same · @PrathapReddy I'm using mongoosejs.com/docs/api/schema.html#schema_Schema-index, a third ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found