You call the index
method on your Schema
object to do that as shown here. For your case it would be something like:
mySchema.index({field1: 1, field2: 1}, {unique: true});
More Related Contents:
- Does MongoDB’s $in clause guarantee order
- Find document with array that contains a specific value
- Moongoose aggregate $match does not match id’s
- What’s Mongoose error Cast to ObjectId failed for value XXX at path “_id”?
- Node.js Mongoose.js string to ObjectId function
- Cascade style delete in Mongoose
- pushing object into array schema in Mongoose
- Mongoose Unique index not working!
- How to push an array of objects into an array in mongoose with one call?
- Find MongoDB records where array field is not empty
- Mongoose — Force collection name
- How do you use Mongoose without defining a schema?
- How to access a preexisting collection with Mongoose?
- How do I query for distinct values in Mongoose?
- Unhandled promise rejection: Error: URL malformed, cannot be parsed
- How to return Mongoose results from the find method?
- MongoDB: output ‘id’ instead of ‘_id’
- How can I update multiple documents in mongoose?
- MongoDB: Calculate dwell time between every status value change
- MongoDB: how to parse date in 3.6 mongoDb version?
- Dealing with schema changes in Mongoose
- Image returned from REST API always displays broken
- Delete a key from a MongoDB document using Mongoose
- Mongoose limit/offset and count query
- Mongoose: deep population (populate a populated field)
- getting schema attributes from Mongoose Model
- Fuzzy Searching with Mongodb?
- MongoDB via Mongoose JS – What is findByID?
- MongoDB count() versus countDocuments()
- Mongodb, aggregate query with $lookup