Skip to content

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

Closed
ccravens opened this issue Sep 5, 2015 · 35 comments
Closed

Feature Request: Allow Unique in Array Schema Definition #3347

ccravens opened this issue Sep 5, 2015 · 35 comments
Labels
Milestone

Comments

@ccravens
Copy link

ccravens commented Sep 5, 2015

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

@vkarpov15 vkarpov15 added this to the 4.3 milestone Sep 8, 2015
@vkarpov15 vkarpov15 modified the milestones: 4.5, 4.4 Feb 1, 2016
@BlueAccords
Copy link

+1
Also, just wondering but how difficult would it be to implement this?

@vkarpov15 vkarpov15 modified the milestones: 4.6, 4.5 Feb 25, 2016
@vkarpov15
Copy link
Collaborator

Pretty difficult actually, mongoose would have to do a lot to make this work, because $addToSet doesn't work well with document arrays. For example, there is no good way to, say, $addToSet a user object to an array called users while enforcing that the username property should be unique. This would make the API a bit unwieldy, because unique would only work as expected for regular arrays, but not work right for document arrays.

@yonib05
Copy link

yonib05 commented Mar 7, 2016

+1
I can imagine it would be a bit complex and inefficient on the querying side but it still would be nice to have.

@trycontrolmymind
Copy link

+1
It would be very nice to do it

@forl
Copy link

forl commented Mar 12, 2016

+1
Nice feature to have!

@ianmin2
Copy link

ianmin2 commented Mar 14, 2016

+1

 That'd be a really awesome feature, especially for use with sub-schemas /documents such that each has it's own distinct scope.  
 Somebody please add it.

@amit3vr
Copy link

amit3vr commented Apr 8, 2016

+1

@faizalpribadi
Copy link

faizalpribadi commented May 24, 2016

+1, but since now, it's a good way, if you handle it with code

@maximilianschmid
Copy link

+1

1 similar comment
@nubitech
Copy link

+1

@klaaz0r
Copy link

klaaz0r commented Sep 14, 2016

+1

4 similar comments
@fauria
Copy link

fauria commented Sep 20, 2016

+1

@scombat
Copy link

scombat commented Oct 13, 2016

+1

@toymachiner62
Copy link

👍

@strafe89
Copy link

+1

@0xc00010ff
Copy link

bump! ✊

@vkarpov15 vkarpov15 modified the milestones: 4.9, 4.8 Jan 2, 2017
@antonsamarsky
Copy link

+1

5 similar comments
@antonlvovych
Copy link
Contributor

+1

@nitindhar7
Copy link

+1

@raphaklaus
Copy link

+1

@bshikhar13
Copy link

+1

@evgeniedasaev
Copy link

+1

@Sonivaibhav26
Copy link

+1

6 similar comments
@jpetitte
Copy link

+1

@rraghav13
Copy link

+1

@robertleeplummerjr
Copy link

+1

@Trenrod
Copy link

Trenrod commented Mar 23, 2017

+1

@pashoo2
Copy link

pashoo2 commented Mar 24, 2017

+1

@januszhou
Copy link

+1

vkarpov15 added a commit to vkarpov15/mongoose-unique-array that referenced this issue Apr 24, 2017
@andrewstec
Copy link

+1

@vkarpov15
Copy link
Collaborator

Use the mongoose-unique-array plugin and mongoose 4.10 for this behavior

@vkarpov15
Copy link
Collaborator

Check out http://thecodebarbarian.com/whats-new-in-mongoose-4.10-unique-in-arrays.html if you're eager to get started 👍

@Pei116
Copy link

Pei116 commented Feb 23, 2018

Does that plugin work with Document.findByIdAndUpdate()?

vkarpov15 added a commit to vkarpov15/mongoose-unique-array that referenced this issue Feb 26, 2018
@vkarpov15
Copy link
Collaborator

@Pei116 no unfortunately it only works for save() right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests