如何使用mongoose从MongoDb获取数据?

编程入门 行业动态 更新时间:2024-10-22 13:52:53
本文介绍了如何使用mongoose从MongoDb获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚开始学习MongoDB和mongoose。目前我有以下结构:

I just started learning MongoDB and mongoose. Currently I have the following structure:

database -> skeletonDatabase collection -> adminLogin

当我运行 db.adminLogin.find()从命令行我得到:

When I run db.adminLogin.find() from the command line I get:

{ "_id" : ObjectId("52lhafkjasfadsfea"), "username" : "xxxx", "password" : "xxxx" }

我的连接

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'); }); }

我的-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 ); }); }

我的 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.

推荐答案

mongoose默认情况下会使用单个模型名称,并将它们与以复数形式命名的集合那么,所以mongoose在数据库中查找一个不存在的名为adminLogins的集合。您可以在定义模式时将集合名称指定为第二个参数:

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:

var adminLogin = new Schema({ username: String, password: String }, {collection: 'adminLogin'});

更多推荐

如何使用mongoose从MongoDb获取数据?

本文发布于:2023-11-22 19:21:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   数据   mongoose   MongoDb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!