views:

194

answers:

1

I've been trying to create a database/model with Mongoose which is basically just a user database where the username is unique. Sounds simple enough, but for some reason I've been unable to do so.

What I've got so far is this:

var mongoose = require('mongoose').Mongoose,
    db = mongoose.connect('mongodb://localhost/db');

mongoose.model('User', {
    properties: [
        'name',
        'age'
    ],

    cast: {
        name: String,
        age: Number
    },

    //indexes: [[{name:1}, {unique:true}]],
    indexes: [
        'name'
    ]
    /*,
    setters: {},
    getters: {},
    methods: {}
    */
});    

var User = db.model('User');

var u = new User();
u.name = 'Foo';

u.save(function() {
    User.find().all(function(arr) {
        console.log(arr);
        console.log('length='+arr.length);
    });
});
/*User.remove({}, function() {});*/

It just doesn't work. The database is created alright, but the username is not unique. Any help or knowledge of what I'm doing wrong?

-freeall

A: 

I'm having the same problem, unique index is not working for me either. I opened an issue, though: http://github.com/LearnBoost/mongoose/issues#issue/56

diogobaeder
Good idea. I stopped using Mongoose and just use mongodb without it. Seems ok for the size of my project.
freeall