当我使用 Mongoose 重新启动我的 NodeJS/Koa.app 时,Mongo 数据被删除

Mongo data being removed when I restart my NodeJS/Koa.app using Mongoose

我遇到了一个问题,当我重新启动 Node/Koa.app 时,存在于我的 Mongo 数据库实例中的任何数据都将被删除。此应用程序使用 Mongoose 连接到本地 Mongo 实例。

这是我的代码:

app.js (I have code in there to output connection to the logger)

import Koa from 'koa';
import path from 'path';
import bodyParser from 'koa-bodyparser';
import serve from 'koa-static';
import mongoose from 'mongoose';
import Config from '../Config.js';

global.appRoot = path.resolve(__dirname);

const app = new Koa();

mongoose.connect(Config.mongo.url);
mongoose.connection.on('connected', (response) => {
    console.log('Connected to mongo server.');
    //trying to get collection names
    let names = mongoose.connection.db.listCollections().toArray(function(err, names) {
        if (err) {
            console.log(err);
        }
        else {
            names.forEach(function(e,i,a) {
                mongoose.connection.db.dropCollection(e.name);
                console.log("--->>", e.name);
            });
        }
    });
});
mongoose.connection.on('error', (err) => {
    console.log(err);
});

上述模块中引用的 MongoDB 配置 url 是:

 mongo: {
        url: 'mongodb://localhost:27017/degould_login'
    }

和我的Mongo模特:

import mongoose from 'mongoose';
const Schema = mongoose.Schema;

let UserSchema = new Schema({
    username: {
        type: String,
        required: true,
        unique: true,
        lowercase: true
    },
    password: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    groupForUsers: [{ type: Schema.Types.ObjectId, ref: 'userGroups' }]
});

export default mongoose.model('users', UserSchema, 'users');

以及插入数据的函数之一

async register(ctx) {

        return new Promise((resolve, reject) => {
            const error = this.checkRequiredVariablesEmpty(ctx, [ 'password', 'email' ]);
            if(error.length) {
                reject(new this.ApiResponse({
                    success: false,
                    extras: {
                        msg: this.ApiMessages.REQUIRED_REGISTRAION_DETAILS_NOT_SET,
                        missingFields: error
                    }}
                ));
            }
            this.userModel.findOne({ email: ctx.request.body.email }, (err, user) => {
                if(err) {
                    reject(new this.ApiResponse({ success: false, extras: { msg: this.ApiMessages.DB_ERROR }}));
                }

                if(!user) {
                    let newUser = new this.userModel();
                    newUser.email = ctx.request.body.email;
                    newUser.username = ctx.request.body.username;
                    newUser.password = ctx.request.body.password;
                    newUser.save()
                        .then((err, insertedRecord) => {

当我启动应用程序并使用注册函数将数据填充到 MongoDB 时,我可以看到数据正确保存到 MongoDB 实例中。

但是,当重新启动应用程序时,所有这些记录都会被删除我的代码中是否有任何原因导致了这种情况?我不可能在开发过程中每次应用程序重启时都必须继续输入数据。

您的问题与此行有关:

mongoose.connection.db.dropCollection(e.name);

...您的 collections 被丢弃在 mongoose 'connected' 事件中。