Skip to content

Instantly share code, notes, and snippets.

@bangbang93
Created November 1, 2018 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bangbang93/f2b0989fb8f218efb14886b85332fd68 to your computer and use it in GitHub Desktop.
Save bangbang93/f2b0989fb8f218efb14886b85332fd68 to your computer and use it in GitHub Desktop.
mongoose-index
import * as Mongoose from 'mongoose'
Mongoose.connect('mongodb://localhost/test')
Mongoose.set('debug', true)
const schema = new Mongoose.Schema({
a: {
type: String,
}
})
schema.index({a: 1}, {unique: true, partialFilterExpression: {$type: 'string'}})
const model = Mongoose.model('t', schema)
async function main() {
const doc = new model({
a: Math.random().toString()
})
await doc.save()
}
main()
.then(() => console.log(123))
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment