Mongoose.connect不创建数据库(如果不存在) [英] Mongoose.connect doesn't create database if not exist

查看:97
本文介绍了Mongoose.connect不创建数据库(如果不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是NodeJ的新手,正在学习如何使用带有Mongoose库的NodeJS连接到mongodb.据我所知,当我使用一个名称连接数据库时,如果该名称的数据库不存在,猫鼬会自动创建它,但是不会与我一起创建它. 这是我的代码:

I'm new on NodeJs, I'm learning how to connect to mongodb using NodeJS with Mongoose library. As I know, when I connect to database with a name, if this database with that name doesn't exist, then mongoose will create it automatically, but it's not being created with me. Here is my code:

const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/mongo-test", {useNewUrlParser: true})
.then(() => console.log("Connected"))
.catch(err => console.log(err));

猫鼬版本:^5.2.5

推荐答案

我不确定100%,但是您还需要创建一条记录,以便它创建数据库. 显然,仅在连接字符串上指定数据库名称是不够的.

I'm not 100% sure but you also need to create a record so that it creates the database. Only specifying the database name on the connection string isn't enough apparently.

欢呼

这篇关于Mongoose.connect不创建数据库(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆