-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Feature Request: Allow Unique in Array Schema Definition #3347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
+1 |
Pretty difficult actually, mongoose would have to do a lot to make this work, because |
+1 |
+1 |
+1 |
+1
|
+1 |
+1, but since now, it's a good way, if you handle it with code |
+1 |
1 similar comment
+1 |
+1 |
4 similar comments
+1 |
+1 |
👍 |
+1 |
bump! ✊ |
+1 |
5 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
6 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
Use the mongoose-unique-array plugin and mongoose 4.10 for this behavior |
Check out http://thecodebarbarian.com/whats-new-in-mongoose-4.10-unique-in-arrays.html if you're eager to get started 👍 |
Does that plugin work with |
@Pei116 no unfortunately it only works for |
The following page states currently supported Schema types: http://mongoosejs.com/docs/schematypes.html
One feature that would be nice is for Mongoose to enforce uniqueness in Arrays, which could guarantee unique values in an array in Mongoose, like the $addToSet functionality in the Mongo database query language.
Current Issue
I can only define an array to hold a list of values, which allows multiple instances of the same value. The following code example demonstrates the issue.
var schema = mongoose.Schema({
friends : [{type : mongoose.Schema.Types.ObjectId, ref : 'User' }]
})
model = new mongoose.model('User')
model['friends'].push(ObjectId('12345'))
model['friends'].push(ObjectId('12345'))
model.save()
Mongo document now looks like:
{friends:[ObjectId('12345'),ObjectId('12345')]}
Suggested Fix
Enhance the schema model to allow unique attribute for an array, which internally uses the Set() object in Mongoose. Then once save() is called, mongoose will convert the set to an array and use the "$addToSet" mongodb operator that will ensure values are unique in the array. Something like the following code snippet would be desired:
var schema = mongoose.Schema({
friends : [{type : mongoose.Schema.Types.ObjectId, ref : 'User', unique: true }]
})
model = new mongoose.model('User')
model['friends'].push(ObjectId('12345'))
model['friends'].push(ObjectId('12345'))
model.save()
Mongo document now looks like:
{friends:[ObjectId('12345')]}
Current Method
Currently it is being suggested to create a seperate update() function with the "$addToSet" parameter, which seems to be more work and burden on the developer than it should be. Please refer to the following:
https://groups.google.com/forum/#!topic/mongoose-orm/QSpr_7rtEYY
http://stackoverflow.com/questions/15921700/mongoose-unique-values-in-nested-array-of-objects
The text was updated successfully, but these errors were encountered: