These are chat archives for Automattic/mongoose

3rd
Apr 2018
warlockD
@warlockdn
Apr 03 2018 20:24

I have these in my schema. Even though I have added index and unique to 3 of them none of them are working ? Can you suggest ?

name: { type: String, required: true },
email: { type: String, index: { unique: true, required: true }, trim: true, lowercase: true, },
phone: { type: Number, index: { unique: true, required: true } },
partnerID: { type: Number, index: { unique: true, required: true }, default: 12000 },

Kev
@lineus
Apr 03 2018 20:28
@warlockdn the index doesn't get created on the collection until the first doc gets saved or until you explicitly call Model.ensureIndexes()
if your initial save to the collection contains multiple docs ( with one ore more that violate the unique index ) it will save them, calls subsequent to that initial save will fail though.
warlockD
@warlockdn
Apr 03 2018 20:34
@lineus hey thanks for quick reply. Where do I add the model.ensureIndexes() ?
Kev
@lineus
Apr 03 2018 20:35
before you create or save any docs. It returns a promise, so you can await it or use it's .then() to wrap subsequent crud operations.
warlockD
@warlockdn
Apr 03 2018 20:35
@lineus oh ok may be that is why its violating and failing. Now is there any other way to do this. I deleted the collections few times but noting works
Kev
@lineus
Apr 03 2018 20:38
@warlockdn when you say it's failing, do you mean that it's saving docs with duplicates on that path? or that you are getting an error back from mongodb?
warlockD
@warlockdn
Apr 03 2018 20:40
@lineus it saves the data with duplicates and I am not getting any errors from mongodb. Can you guide me to a resource online so that I can read and do it ?
Kev
@lineus
Apr 03 2018 20:42
here is the link to the ensureIndexes api docs. The example they use listens for the index event but you can use the promise as well.
Kev
@lineus
Apr 03 2018 20:53
I remember being able to save multiple versions of the same unique field at some point in the past, but I tried to duplicate it now and I get an error.
@warlockdn here is a gist
looking closer at your path declarations above, I'm pretty sure that your syntax is off just a bit. the index key is actually just a boolean on schematypes. check my gist for the proper syntax.
Kev
@lineus
Apr 03 2018 20:58
here are the docs on schematypes, scroll about halfway down (under schematype options) for indexes. When the Model compiles that schema, it's just coercing the object you're passing in as truthy and creating an index, but it's not a unique one. you can inspect the indexes that are created on your collection in the mongo shell, with the native driver, or with mongoose.
warlockD
@warlockdn
Apr 03 2018 20:59
@lineus should I just delete the collection and start again ? I am not sure if anything I am doing is getting reflected anywhere
Kev
@lineus
Apr 03 2018 21:00
that's entirely dependent on the data that's in there. if it's just a test db and you can afford to lose the data, that's what I would do.
warlockD
@warlockdn
Apr 03 2018 21:01
I deleted many times nothing works. the doc says clearly just add it to the schema type options and I did that still the issue
Kev
@lineus
Apr 03 2018 21:01
what doc are you looking at?
warlockD
@warlockdn
Apr 03 2018 21:02
under indexes (You can also define MongoDB indexes using schema type options.)

here is the updated schema

partnerID: { type: Number, index: true, unique: true, required: true, default: 12000 },
name: { type: String, required: true },
email: { type: String, index: true, unique: true, required: true, trim: true, lowercase: true, },

still the same
warlockD
@warlockdn
Apr 03 2018 21:07

@lineus i put debug mode true and this is the message I receive

Mongoose: identitycounters.ensureIndex({ field: 1, model: 1 }, { unique: true, required: true, index: -1, background: true })

Kev
@lineus
Apr 03 2018 21:08
can you share a link to a gist of your code? when I try it with the index created like yours it still works correctly for me ( error e11000 )
what version of mongodb and mongoose are you using?
warlockD
@warlockdn
Apr 03 2018 21:12
here gist
Kev
@lineus
Apr 03 2018 21:16
what version of mongoose are you using?
warlockD
@warlockdn
Apr 03 2018 21:16
5.0.11
Kev
@lineus
Apr 03 2018 21:19
that plugin you're using hasing been maintained in a very long time, let me add it to my example and see if changes anything.
warlockD
@warlockdn
Apr 03 2018 21:20
@lineus which plugin ?
Kev
@lineus
Apr 03 2018 21:21
mongoose-auto-increment
warlockD
@warlockdn
Apr 03 2018 21:22
i checked around a lot many of them are using it. anyways is that's what causing the issue ?
Kev
@lineus
Apr 03 2018 21:24
oh, you're also adding your plugin after the schema has been compiled by mongoose.model.
try adding the plugin, and then calling mongoose.model
const Partner = mongoose.model('Partner', partnerSchema); should be after your plugins and hooks
and you might want to call connection.model instead, so that you're explicitly using the connection that the plugin is "initialized" on. I think since you have only one connection, it might work as is, but might as well be explicit about it.
warlockD
@warlockdn
Apr 03 2018 21:27
@lineus nope dude. still the same.
ya i tried that. but the plugin says I gotta make a connection and initialize it. Or can I use the existing connection ?
Kev
@lineus
Apr 03 2018 21:30
that's what I mean, instead of calling mongoose.model like you are currently, call connection.model instead
just going off of the docs from that plugin
fwiw, it looks like someone has created a more recently maintained fork of that plugin, mentioned chevex-archived/mongoose-auto-increment#82
warlockD
@warlockdn
Apr 03 2018 21:31
so I just replace and it should work ?
Kev
@lineus
Apr 03 2018 21:32
god I hope so
:)
warlockD
@warlockdn
Apr 03 2018 21:36
lemme try
haha. it worked. the plugin was the issue here.
thank you @lineus. you saved me a headache.
Kev
@lineus
Apr 03 2018 21:38
awesome!
happy to help :)
warlockD
@warlockdn
Apr 03 2018 21:39
:)
Kev
@lineus
Apr 03 2018 21:40
@warlockdn that newer fork of the plugin doesn't have the initialization step according to the readme. it might be worth trying that one instead.
warlockD
@warlockdn
Apr 03 2018 21:40
yea I used the new plugin. the connection is used to do the model
so that works perfectly
Kev
@lineus
Apr 03 2018 21:41
nice
warlockD
@warlockdn
Apr 03 2018 21:42
u got like 5 mins more ? I had a doubt i wanted to clear.
@lineus
Kev
@lineus
Apr 03 2018 21:46
what's up?