在Express中哪里可以找到Mongo Collection?

时间:2019-09-18 03:54:44

标签: mongodb express mongoose

我正在查看我在Express / Mongo / Mongoose中编写的一些旧代码(基于在线教程),并且找不到定位于MongoDB中哪个Collection的代码部分。

我有一个数据库UsersDB,并且在该数据库中有多个集合。每次代码执行user.save时保持增长的Collection是users Collection。但是我在代码库中找不到对users集合的引用。

代码中保存User的唯一位置是:


var app = require('../app');
var util = require('util');
var User = require('../models/user'),
    Auth = User.Auth,
    Name = User.Name,
    Email= User.Email,
    Phone = User.Phone,
    Address = User.Address,
    Company = User.Company,
    PersonalData = User.PersonalData,
    Id = User.Id,
    Photo = User.Photo,
    Member = User.Member,
    CreditCard = User.CreditCard,
    UserObj = User.User;

var moment = require('moment');

var async = require('async');

. . . 
. . . 

exports.user_create_post = [
    (req,res, next) => {
        console.log("Request: " + util.inspect(req.body));
    },
    //VALIDATE
    body('mainEmail', 'Must be valid email.').isLength({min: 5}).trim(),

    //SANITIZE
    sanitizeBody('*').escape(),

    //POPULATE NEW DOCUMENT
    (req,res,next) => {
        const errors = validationResult(req);

        var auth = new Auth(
                dateEffective: {value: moment(Date.now()).format("YYYY-MM-DD hh:mm:ss SSS"), attr: {hidden: true, label: ""}},
                username: {"value": req.body.username, "attr": {hidden: false, label: "Username: "}},
                password: {"value": req.body.password, "attr": {hidden: false, label: "Password: "}},
                mainEmail: {"value": req.body.mainEmail, "attr": {hidden: false, label: "Email: "}}
            });
        var user = new UserObj(
            {authData: [auth]}
        );
        if (!errors.isEmpty()) {
            const errorFormatter = ({ location, msg, param, value, nestedErrors }) => {
                // Build your resulting errors however you want! String, object, whatever - it works!
                return `${location}[${param}]: ${msg}`;
              };
              const result = validationResult(req).formatWith(errorFormatter);
              if (!result.isEmpty()) {
                return res.json({ errors: result.array() });
              }
            }
            else {
                user.save(function(err){
                    if (err) { return next(err);}
                });
                res.redirect("http://localhost:4200/two-fa/"+user._id);
        }
    } 

我还有一个Models模块(user.js):


. . . 
. . . 
. . . 

module.exports = {
    Auth: mongoose.model('Auth', AuthSchema),
    Name: mongoose.model('Name', NameSchema),
    Email: mongoose.model('Email', EmailSchema),
    Phone: mongoose.model('Phone', PhoneSchema),
    Address: mongoose.model('Address', AddressSchema),
    Company: mongoose.model('Company', CompanySchema),
    PersonalData: mongoose.model('PersonalData', PersonalDataSchema),
    Id: mongoose.model('Id', IdSchema),
    Photo: mongoose.model('Photo', PhoneSchema),
    Member: mongoose.model('Member', MemberSchema),
    CreditCard: mongoose.model('CreditCard', CreditCardSchema),
    User: mongoose.model('User', UserSchema)
}

我在整个代码中进行了搜索,没有地方提到users,这是正在写入的集合。

我应该在哪里尝试跟踪users集合的写入方式?

谢谢!

1 个答案:

答案 0 :(得分:0)

根据how-to-access-a-preexisting-collection-with-mongoose,如果未提供mongoose.model的第三个参数,则Mongo会自动“复数化”模型名称以提供集合的名称。