I have this code, but it does not enforce the array be unique.
/**
* User Schema
*/
var UserSchema = new Schema({
username: { type: String, unique: true },
followers: [{ type: Schema.ObjectId, ref: 'User', index: true, unique: true }],
following: [{ type: Schema.ObjectId, ref: 'User', index: true, unique: true }]
});
I can add the same user to followers
or following
multiple times without any errors. It should throw an error if there is already that user in the list.
Is there a better way to provide uniqueness on the Schema.ObjectId, ref: 'User'
arrays?
Someone mentioned I should look into $addToSet, but I cannot find any examples of how to do this with Mongoose.
...
following: [{ type: Schema.ObjectId, ref: 'User', index: {unique: true, dropDups: true} }]
...
UserSchema.index({following: 1}, {unique: true, dropDups: true});
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.