node.js - 无法连接到 MongoDB 并通过 Node / Postman 创建新的 Collections 文件夹
问题描述
我正在关注这个 Youtube 视频的教程,以便构建一个可以与我的 Mongo 数据库连接和交互的 API。在我到达链接中引用的部分(1:05:20)之前,进展非常顺利。
这是教程创建者使用的代码。
let mongoose = require('mongoose')
const server = 'ds221609.mlab.com:21609'
const database = 'rest-api-workshop'
const user = 'username'
const password = 'password'
mongoose.connect(`mongodb://${user}:${password}@${server}/${database}`)
let CustomerSchema = new mongoose.Schema({
name: String,
email: {
type: String,
requried: true,
unique: true
}
})
module.exports = mongoose.model('Customer', CustomerSchema)
我显然用我的 mongodb 用户的凭据交换了凭据,但仍然无法让它工作。此外,mongodb 给出的格式现在看起来非常不同:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@<databaseID>.azure.mongodb.net/admin?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
我的目标是基本上在我的数据库集合的“客户”文件夹下创建一个新记录。我如何在 2019 年实现这一目标?
解决方案
推荐阅读
- bash - 使用第一行作为标题并按列迭代的 AWK 格式
- windows - Windows 10 Enterprise VPN 连接属性中缺少“证书”选项
- java - Spring Boot REST API 提供空行
- javascript - 目标第一个数组,数组数组中的第一个值
- video.js - 寻找适用于 videojs 7.5 的 HLS/MPEG-DASH 质量选择器
- vue.js - Vue.js 使用 $route.name 隐藏组件的一部分
- javascript - 为什么jQuery脚本会使输入对象中加载的值消失?
- python-3.x - AttributeError:模块 'whois' 没有属性 'whois' [库已安装] - Python 3.6.7
- redux - 当我们在循环中使用 Redux-Saga all effect 时如何处理错误?
- kotlin - 仅当调用观察者方法时才返回值