猫鼬实际上在这里打开了什么?
What is mongoose actually opening here?
我正处于学习 MEAN 堆栈的初级阶段。
我使用 mongoose 和 mongodb 创建了我的第一个 Node 应用程序。
在我的 node.js 代码中,我有以下内容:
mongoose.connect('mongodb://localhost/meantest1');
我可以创建文档并很好地找到它们,但是,我不完全确定它存储数据的位置?
app.get('/api/test', function (req, res) {
test.create({
name: "Joe Schmo"
}, function() {
test.find(function (err, tests) {
res.json(tests);
})
});
})
在mongoshell中,如果我执行
use meantest1
db.test.find()
这是我在 shell 中输入的 returns 数据,但我的应用程序中没有任何数据。
谁能解释一下这是怎么回事?
此外,有没有比 shell 更好的查询 mongo 的应用程序??
Mongoose 在您注册模型时自动将集合名称复数化。在您的情况下,模型名称 Test
将转换为集合名称 tests
。
如果您想防止复数化,或者您需要将您的模型附加到现有的非复数化集合,您可以将集合名称作为第三个参数传递给 mongoose.model
方法:
mongoose.model('Test', testSchema, 'test');
我正处于学习 MEAN 堆栈的初级阶段。
我使用 mongoose 和 mongodb 创建了我的第一个 Node 应用程序。
在我的 node.js 代码中,我有以下内容:
mongoose.connect('mongodb://localhost/meantest1');
我可以创建文档并很好地找到它们,但是,我不完全确定它存储数据的位置?
app.get('/api/test', function (req, res) {
test.create({
name: "Joe Schmo"
}, function() {
test.find(function (err, tests) {
res.json(tests);
})
});
})
在mongoshell中,如果我执行
use meantest1
db.test.find()
这是我在 shell 中输入的 returns 数据,但我的应用程序中没有任何数据。
谁能解释一下这是怎么回事?
此外,有没有比 shell 更好的查询 mongo 的应用程序??
Mongoose 在您注册模型时自动将集合名称复数化。在您的情况下,模型名称 Test
将转换为集合名称 tests
。
如果您想防止复数化,或者您需要将您的模型附加到现有的非复数化集合,您可以将集合名称作为第三个参数传递给 mongoose.model
方法:
mongoose.model('Test', testSchema, 'test');