首页 > 解决方案 > 创建集合后如何设置“上限”?

问题描述

我试图capped在我的 mongoose.Schema 中为我的集合设置一个参数,该参数最初不包括在内capped。欢迎任何帮助。

我的架构:

    const mongoose = require('mongoose')
    
    var Schema = mongoose.Schema;
    var userSchema = new Schema({
        name:  { type: String, required: true },
        email: { type: String },
        password: { type: String },
        isAdmin: {type: Boolean, default: false},
        avatar: { type: String },
        joinDate: { type: Date, default: Date.now() },
    },{ autoCreate: true, capped : 1024})

userSchema.set('timestamps', true);
const Users = mongoose.model('Users', userSchema)
module.exports = Users;

我收到以下错误:

Error: A non-capped collection exists with the name: users  
To use this collection as a capped collection, please first convert it.

标签: node.jsmongodbmongoose

解决方案


好像您已经users在数据库中创建了一个集合。因此,要将其转换为以下命令的上限运行mongoshellrobomongo

db.runCommand( { convertToCapped: 'users', size: 1024 } )

推荐阅读