按索引Mongoose设置数组项

时间:2015-02-08 01:22:47

标签: node.js mongodb mongoose database-schema

我有一个看起来像这样的Mongoose Schema

var aSchema = new mongoose.Schema({
    type: String,
    id: String,
    registered: [{
        user: String,
        priority: String,
        id: String
    }]
});

我想在registered属性中包含14个项目,并希望能够单独设置它们。当用户提交表单时,我正在查询/更新集合,如此

aSchema.findOneAndUpdate({id: some_id}, {
        type: 'a type',
        id: 'an id'
    },
    { upsert: true },
    function (err, model)
    {
        var endIndex = 4;
        var startIndex = 2;

        for (i=startIndex;i<endIndex;i++)
        {
            model.registered[i] = {
                user: req.session.user,
                priority: req.body.priority,
                id: req.body.token
            }
        }

        model.save(function ()
        {
            //Do some more
        });
   }
);

当我运行上述代码的变体时,我得到了

doc.validate(function (err) {
    ^
TypeError: Object #<Object> has no method 'validate'

我认为这是因为我试图用数组做的奇怪事情。有没有办法解决这个问题?

0 个答案:

没有答案