javascript - Mongoose 没有在 save() 函数上创建集合
问题描述
我有以下架构。
const mongoose = require('mongoose');
const JobSchema = new mongoose.Schema({
posterId: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
location: {
type: String,
required: true,
},
}, { timestamp: true });
module.exports = mongoose.model('Job', JobSchema);
我使用下面的代码将新数据保存到这个集合中。
const Job = require('../../models/Job');
new Job({
posterId,
title,
location,
})
.save()
.then(job => {
console.log('JOB: ', job) // consoles the newly added job to collection.
if (job) {
response.json({
message: 'Job is saved successfully.',
});
} else {
response.json({
message: 'Job can not be saved at this time. Please try again later.',
});
}
})
.catch(error => {
response.json(error);
});
上面的代码成功地将新信息保存到作业集合中,但是当我使用 mongodb CLI 在 mongoDB 中检查时。我在那里看不到任何工作集合,因此它没有创建集合。我做错了什么,我该如何解决?
解决方案
推荐阅读
- javascript - 检测 JSON 存在
- c# - 在测试类中测试私有方法
- r - r - 无论图例宽度如何,如何设置绘图大小 ggplot2
- android - Android如何在文本视图行上设置自定义宽度
- swift - Swift - UITableView 插入行导致死锁
- javascript - 在 RMarkdown 演示文稿中滚动幻灯片 (revealjs)
- android-studio - 在 gmail 应用程序中,日历和通讯录可以单独打开。我们如何从主应用程序打开另一个应用程序
- maven - 使用 SQL 和 MVN 的 Liquibase 上下文
- testng - Testng 为通用测试方法动态更改测试名称
- syslog - 将消息转发到 syslog 服务器