Mongo Mongoose 索引后的数据库固定名称?
Mongo Database Fixing Names after Mongoose Index?
我在 Mongoose 与我的节点和 Mongo 数据库交互的方式上遇到了一个非常奇怪的问题。
我正在使用 express 创建一个基本的 get api 路由来从我的 mongodb 中获取一些数据。
我有一个名为 test 的数据库,它有一个 collection 调用 "billings"
所以架构和路由非常基础
apiRouter.route('/billing/')
.get(function(req, res) {
Billing.find(function(err, billings) {
if (err) res.send(err);
// return the bills
res.json(billings);
});
});
其中 "Billing" 是我的 mongoose 架构。只有 1 object {test: string}
这很好用,我收到了一个回复,其中包含我的 mongo 数据库中名为 "billings" 的所有项目,这只是一个项目 {test: "success"}
接下来我创建了一个名为 "historys"
的 collection
我设置的设置与我的账单完全相同。
apiRouter.route('/historys/')
// get all the history
.get(function(req, res) {
Historys.find(function(err, historys) {
if (err) res.send(err);
// return the history
res.json(historys);
});
});
"Historys" 又是我的 mongoose 架构。这个模式在设置上与我的账单相同,因为我没有任何真实数据,字段是相同的,我只是有一个测试字段,所以从账单和历史返回的 json object 应该已经
{ test: "success" }
然而,这次我没有得到任何数据,我只是得到一个空的object
[].
我多次检查我的代码以确保可能丢失了一个 capital 或某处的逗号等,但代码是相同的。我的 mongodb 中的设置和格式是相同的。我进入 robomongo 并查看了数据库,一切都被正确命名。
除了,我现在有 2 个新的 collection。
我的原版:"Historys" 和一个全新的 collection "Histories"
一旦我修复了我的 api 路线去查看历史记录而不是历史记录,我就能够成功获得测试数据。但是,我仍然无法从历史记录中提取数据,它就像它不存在一样,但当我刷新时它就在我的 robomongo 控制台中。
我在所有代码中搜索了任何提及历史的内容,但得到 0 个结果。系统哪里知道要修复我的 collection 上的语法?
来自文档:
When no collection argument is passed, Mongoose produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. If you don't like this behavior, either pass a collection name or set your schemas collection name option.
所以,当您这样做时,在您的模式定义中,这:
mongoose.model('Historys', YourSchema);
,mongoose 创建了 Histories
集合。
当你这样做时:
db.historys.insert({ test: "success" })
通过 mongodb
控制台,如果 historys
集合不存在,将创建它。这就是为什么你的数据库中有两个集合。正如文档所说,如果您不想 mongoose
根据您的模型创建一个具有复数名称的集合,只需指定您想要的名称即可。
我在 Mongoose 与我的节点和 Mongo 数据库交互的方式上遇到了一个非常奇怪的问题。
我正在使用 express 创建一个基本的 get api 路由来从我的 mongodb 中获取一些数据。
我有一个名为 test 的数据库,它有一个 collection 调用 "billings"
所以架构和路由非常基础
apiRouter.route('/billing/')
.get(function(req, res) {
Billing.find(function(err, billings) {
if (err) res.send(err);
// return the bills
res.json(billings);
});
});
其中 "Billing" 是我的 mongoose 架构。只有 1 object {test: string}
这很好用,我收到了一个回复,其中包含我的 mongo 数据库中名为 "billings" 的所有项目,这只是一个项目 {test: "success"}
接下来我创建了一个名为 "historys"
的 collection我设置的设置与我的账单完全相同。
apiRouter.route('/historys/')
// get all the history
.get(function(req, res) {
Historys.find(function(err, historys) {
if (err) res.send(err);
// return the history
res.json(historys);
});
});
"Historys" 又是我的 mongoose 架构。这个模式在设置上与我的账单相同,因为我没有任何真实数据,字段是相同的,我只是有一个测试字段,所以从账单和历史返回的 json object 应该已经
{ test: "success" }
然而,这次我没有得到任何数据,我只是得到一个空的object
[].
我多次检查我的代码以确保可能丢失了一个 capital 或某处的逗号等,但代码是相同的。我的 mongodb 中的设置和格式是相同的。我进入 robomongo 并查看了数据库,一切都被正确命名。
除了,我现在有 2 个新的 collection。
我的原版:"Historys" 和一个全新的 collection "Histories"
一旦我修复了我的 api 路线去查看历史记录而不是历史记录,我就能够成功获得测试数据。但是,我仍然无法从历史记录中提取数据,它就像它不存在一样,但当我刷新时它就在我的 robomongo 控制台中。
我在所有代码中搜索了任何提及历史的内容,但得到 0 个结果。系统哪里知道要修复我的 collection 上的语法?
来自文档:
When no collection argument is passed, Mongoose produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. If you don't like this behavior, either pass a collection name or set your schemas collection name option.
所以,当您这样做时,在您的模式定义中,这:
mongoose.model('Historys', YourSchema);
,mongoose 创建了 Histories
集合。
当你这样做时:
db.historys.insert({ test: "success" })
通过 mongodb
控制台,如果 historys
集合不存在,将创建它。这就是为什么你的数据库中有两个集合。正如文档所说,如果您不想 mongoose
根据您的模型创建一个具有复数名称的集合,只需指定您想要的名称即可。