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.
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')]}
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')]}
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
+1
+1
Also, just wondering but how difficult would it be to implement this?
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.
+1
I can imagine it would be a bit complex and inefficient on the querying side but it still would be nice to have.
+1
It would be very nice to do it
+1
Nice feature to have!
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.
+1
+1, but since now, it's a good way, if you handle it with code
+1
+1
+1
+1
+1
👍
+1
bump! ✊
+1
+1
+1
+1
+1
+1
+1
+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 :+1:
Does that plugin work with Document.findByIdAndUpdate()
?
@Pei116 no unfortunately it only works for save()
right now
Most helpful comment
Check out http://thecodebarbarian.com/whats-new-in-mongoose-4.10-unique-in-arrays.html if you're eager to get started :+1: