These are chat archives for Automattic/mongoose

23rd
Feb 2017
Luke Gordon
@gordysc
Feb 23 2017 02:55
Hey folks, has anyone used mongoose with a Mongo cluster? I'm having issues autocreating indexes for my schema on a fresh cluster. If I point at a single instance Mongoose is creating the indexes appropriately, but not against a replica set (3 instances).
akilesh
@akileshv
Feb 23 2017 06:12
{name: {type: String, unique: true}} this allows me to enter duplicate values, how can i avoid insertion of duplicate value
Paul "Joey" Clark
@joeytwiddle
Feb 23 2017 08:24
@akhileshv That rule should prevent the insertion of duplicate values. It works by creating an index in Mongo.
If it isn’t working, then it is likely that Mongo was unable to create the index because (for example) you already had some duplicates in your collection.
You can check if that is the case by adding an error listener. I recommend doing this for all your models:
        Model.on('index', function (err) {
            if (err) {
                console.warn("Indexing error: " + err);
            }
        });
Paul "Joey" Clark
@joeytwiddle
Feb 23 2017 08:46
If you see the error, then you will need to drop the documents with duplicate values, or fix those fields, or drop the entire collection. After that mongoose will try to recreate the index on the next startup.
@seltsam23 I don’t believe it’s possible out of the box. But there is a module you could try called mongoose-readonly.
It looks like it will apply to document.save() operations but not to Model.update() operations.
Paul "Joey" Clark
@joeytwiddle
Feb 23 2017 09:53
Interesting aside: Mongo allows us to create views, which can be used to give readonly access to some users. They look like collections when querying, but insert/update operations on them will fail. It won’t help you to lock individual fields though.