Feature Request: Allow Unique in Array Schema Definition #3347

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

Comments

Projects
None yet
@ccravens

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

@filfat

This comment has been minimized.

Show comment
Hide comment

filfat commented Feb 25, 2016

+1

@BlueAccords

This comment has been minimized.

Show comment
Hide comment
@BlueAccords

BlueAccords Feb 25, 2016

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

+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

This comment has been minimized.

Show comment
Hide comment
@vkarpov15

vkarpov15 Feb 25, 2016

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.

Collaborator

vkarpov15 commented Feb 25, 2016

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

This comment has been minimized.

Show comment
Hide comment
@yonib05

yonib05 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.

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.

@vlaad360

This comment has been minimized.

Show comment
Hide comment
@vlaad360

vlaad360 Mar 7, 2016

+1
It would be very nice to do it

vlaad360 commented Mar 7, 2016

+1
It would be very nice to do it

@forl

This comment has been minimized.

Show comment
Hide comment
@forl

forl Mar 12, 2016

+1
Nice feature to have!

forl commented Mar 12, 2016

+1
Nice feature to have!

@ianmin2

This comment has been minimized.

Show comment
Hide comment
@ianmin2

ianmin2 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.

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

This comment has been minimized.

Show comment
Hide comment

amit3vr commented Apr 8, 2016

+1

@faizalpribadi

This comment has been minimized.

Show comment
Hide comment
@faizalpribadi

faizalpribadi May 24, 2016

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

faizalpribadi commented May 24, 2016

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

@maximilianschmid

This comment has been minimized.

Show comment
Hide comment
@nubitech

This comment has been minimized.

Show comment
Hide comment

+1

@vkarpov15 vkarpov15 added the plugin label Jun 25, 2016

@klaaz0r

This comment has been minimized.

Show comment
Hide comment

klaaz0r commented Sep 14, 2016

+1

@fauria

This comment has been minimized.

Show comment
Hide comment

fauria commented Sep 20, 2016

+1

@scombat

This comment has been minimized.

Show comment
Hide comment

scombat commented Oct 13, 2016

+1

@toymachiner62

This comment has been minimized.

Show comment
Hide comment

👍

@strafe89

This comment has been minimized.

Show comment
Hide comment

+1

@0xc00010ff

This comment has been minimized.

Show comment
Hide comment

bump!

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

@antonsamarsky

This comment has been minimized.

Show comment
Hide comment

+1

@geekiam

This comment has been minimized.

Show comment
Hide comment
@geekiam

geekiam Jan 12, 2017

Contributor

+1

Contributor

geekiam commented Jan 12, 2017

+1

@nitindhar7

This comment has been minimized.

Show comment
Hide comment

+1

@raphaklaus

This comment has been minimized.

Show comment
Hide comment

+1

@bshikhar13

This comment has been minimized.

Show comment
Hide comment

+1

@dasaeve

This comment has been minimized.

Show comment
Hide comment

dasaeve commented Feb 11, 2017

+1

@Sonivaibhav26

This comment has been minimized.

Show comment
Hide comment
@jpetitte

This comment has been minimized.

Show comment
Hide comment

+1

@raghav130593

This comment has been minimized.

Show comment
Hide comment

+1

@robertleeplummerjr

This comment has been minimized.

Show comment
Hide comment
@Trenrod

This comment has been minimized.

Show comment
Hide comment

Trenrod commented Mar 23, 2017

+1

@pashoo2

This comment has been minimized.

Show comment
Hide comment

pashoo2 commented Mar 24, 2017

+1

@januszhou

This comment has been minimized.

Show comment
Hide comment

+1

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

@andrewstec

This comment has been minimized.

Show comment
Hide comment

+1

@vkarpov15

This comment has been minimized.

Show comment
Hide comment
@vkarpov15

vkarpov15 May 18, 2017

Collaborator

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

Collaborator

vkarpov15 commented May 18, 2017

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

@vkarpov15

This comment has been minimized.

Show comment
Hide comment
Collaborator

vkarpov15 commented May 19, 2017

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

@Pei116

This comment has been minimized.

Show comment
Hide comment
@Pei116

Pei116 Feb 23, 2018

Does that plugin work with Document.findByIdAndUpdate()?

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

This comment has been minimized.

Show comment
Hide comment
@vkarpov15

vkarpov15 Feb 26, 2018

Collaborator

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

Collaborator

vkarpov15 commented Feb 26, 2018

@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