如何在Mongoose中设置数据库名称和集合名称?

e5nqia27  于 2022-11-13  发布在  Go
关注(0)|答案(3)|浏览(166)

我正在创建一个连接到MongoDB-Atlas的小程序。我创建了一个连接、一个模式、一个模型并创建了一个文档。但不知何故,我的DB名称是“test”,而Collection名称是“users”,而我没有在代码或Atlas中定义它,这是默认名称吗?以及如何创建/重命名DB和Collection。
代码:

使用者.js

const mongoose = require('mongoose');
const SchemaObj = mongoose.Schema;
const userSchema = new SchemaObj({
    name : {
        type:String,
        require:true

    },
    email : {
        type:String,
        require:true
    },
    password: {
        type:String,
        require:true
    }
});

mongoose.model('User',userSchema);

应用程序.js

const express = require('express');
const app = express();
const rout = express.Router();
const PORT = 8888;
const {connectionString} = require('./Keys');
const mongoose = require('mongoose');

app.use(express.json());
app.use(require('./routes/auth'));

app.get('/',(req,res)=>{
    console.log('succesfully connected');
    res.send('im in!');
});

let server = app.listen(PORT,'0.0.0.0',()=>{
    let FuncPort = server.address().port
    let host = server.address().address
    console.log("Example app listening at http://%s:%s", host, FuncPort)
});

const client = mongoose.connect(connectionString, {
  useNewUrlParser: true,
  useUnifiedTopology: true
});
    
mongoose.connection.on('connected',()=>{
    console.log('connected to mongodb oh hell yea');
});

mongoose.connection.on('error',()=>{
    console.log('error connecting to mongodb oh hell yea');
});

验证js

const mongoose = require('mongoose');
const express = require('express');
const route = express.Router();
require('../moduls/User');
const user = mongoose.model("User");

rout.post('/sign',(req,res)=>{
    const {name,password,email} = req.body;
    if(name && password && email) {
        console.log('you god damn right!');
        res.json("you god damn right in response");
    } else {
        console.log('you are god damn wrong stupid idiot!');
        res.status(422);
        res.json("you are god damn wrong you stupid idiot in response");
    }

    user.findOne({email:email}).then((resolve,reject)=>{
        if(resolve)
            return res.status(422).json("user already exist yo");
        
        const newUser = new user({ name, email, password });
        newUser.save().then(() => {
            res.json('saved user!!!!');
        }).catch(err => {
            console.log("there was a problem saving the user")});
        }).catch(err => {  
            console.log(err);
        })
});
module.exports = route;

顺便问一下,mongooseMongoDB库之间有什么区别?

rvpgvaaj

rvpgvaaj1#

为了命名mongodb数据库,你应该把它放在你的连接字符串中,比如:

mongoose.connect('mongodb://localhost/myDatabaseName');

对于命名您的收藏或禁用复数,您可以在这里找到答案:
What are Mongoose (Nodejs) pluralization rules?

var schemaObj = new mongoose.Schema(
{
 fields:Schema.Type
}, { collection: 'collection_name'});

Mongoose是MongoDB和Node.js的对象数据建模(ODM)库。类似于Sequelize(ORM),但用于文档(NoSQL)。

icnyk63a

icnyk63a2#

(添加名称数据库)?重试写入=true&w=majority
您可以只添加您想要的名称(ADD NAME),并删除测试数据库和重新加载

iih3973s

iih3973s3#

方法1

const url = "mongodb://127.0.0.1:27017/DatabaseName"

mongoose.connect(url).then(() => {
    console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

方法2

const url = "mongodb://127.0.0.1:27017"

mongoose.connect(url,{
    dbName: 'DatabaseName',
}).then(() => {
    console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

相关问题