node.js 如何使用猫鼬从 MongoDb 获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19627631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Data from MongoDb using mongoose?
提问by Kris Hollenbeck
I just started learning MongoDB and mongoose. Currently I have the following structure:
我刚开始学习 MongoDB 和 mongoose。目前我有以下结构:
database -> skeletonDatabase
collection -> adminLogin
When I run db.adminLogin.find()from the command line I get:
当我从命令行运行时db.adminLogin.find(),我得到:
{ "_id" : ObjectId("52lhafkjasfadsfea"), "username" : "xxxx", "password" : "xxxx" }
My connection(this works, just adding it FYI)
我的连接(这有效,只是添加它仅供参考)
module.exports = function(mongoose)
{
mongoose.connect('mongodb://localhost/skeletonDatabase');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('Conntected To Mongo Database');
});
}
My -js-
我的-js-
module.exports = function(mongoose)
{
var Schema = mongoose.Schema;
// login schema
var adminLogin = new Schema({
username: String,
password: String
});
var adminLoginModel = mongoose.model('adminLogin', adminLogin);
var adminLogin = mongoose.model("adminLogin");
adminLogin.find({}, function(err, data){
console.log(">>>> " + data );
});
}
My console.log()returns as >>>>
我的console.log()回报为>>>>
So what am I doing wrong here? Why do I not get any data in my console log? Thanks in advance for any help.
那么我在这里做错了什么?为什么我的控制台日志中没有任何数据?在此先感谢您的帮助。
回答by Peter Lyons
mongoose by default takes singular model names and pairs them with a collection named with the plural of that, so mongoose is looking in the db for a collection called "adminLogins" which doesn't exist. You can specify your collection name as the 2nd argument when defining your schema:
mongoose 默认情况下采用单数模型名称并将它们与一个以复数命名的集合配对,因此 mongoose 正在数据库中查找一个名为“adminLogins”的集合,该集合不存在。您可以在定义架构时将集合名称指定为第二个参数:
var adminLogin = new Schema({
username: String,
password: String
}, {collection: 'adminLogin'});
回答by elkhrz
first compile just onemodel with the schema as an argument
首先以模式作为参数编译一个模型
var adminLogin = mongoose.model('adminLogin', adminLogin);
var adminLogin = mongoose.model('adminLogin', adminLogin);
in your code adminLogin does not exist, adminLoginModel does;
在您的代码中 adminLogin 不存在, adminLoginModel 存在;
after that ,instead to
在那之后,而不是
adminLogin.find({}, function(err, data){
console.log(">>>> " + data );
});
try this
尝试这个
adminLogin.find(function (err, adminLogins) {
if (err) return console.error(err);
console.log(adminLogins);
is important the "s" because mongo use the plural of the model to name the collection, sorry for my english...
“s”很重要,因为 mongo 使用模型的复数来命名集合,对不起我的英语......
回答by DevOpsIsTheNameOfTheGame
Had a problem with injecting it within an express route for my api so I changed it thanks to @elkhrz by first defining the schema and then compiling that one model I want to then pull like so:
在将它注入到我的 api 的快速路由中时遇到了问题,所以我通过首先定义模式然后编译我想要的模型然后像这样拉取@elkhrz 来更改它:
app.get('/lists/stored-api', (req, res) => {
Apis.find(function(err, apis) {
if (err) return console.error(err);
res.send(apis);
});
});
I wouldn't send it to the body, I would actually do something else with it especially if you plan on making your API a production based application.
我不会将它发送给正文,我实际上会用它做其他事情,尤其是如果您打算将 API 设为基于生产的应用程序。
Run through this problem and read up on possible proper ways of rendering your data: How to Pass Data Between Routes in Express
解决这个问题并阅读可能的正确呈现数据的方法: 如何在 Express 中的路由之间传递数据
Always a good idea to practice safe procedures when handling data.
在处理数据时实践安全程序始终是一个好主意。

