Mongoose 总是返回一个空数组 NodeJS [英] Mongoose always returning an empty array NodeJS

查看:25
本文介绍了Mongoose 总是返回一个空数组 NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 findfindOne 并且都没有返回文档.find 返回一个空数组,而 findOne 返回 null.errnull 两种情况下也是如此.

I have tried using find and findOne and both are not returning a document. find is returning an empty array while findOne is returning null. err in both cases in null as well.

这是我的连接:

function connectToDB(){
    mongoose.connect("mongodb://localhost/test"); //i have also tried 127.0.0.1
    db = mongoose.connection;
    db.on("error", console.error.bind(console, "connection error:"));
    db.once("open", function callback(){
        console.log("CONNECTED");
    });
};

这是我的架构:

var fileSchema = mongoose.Schema({
    hash: String,
    type: String,
    extension: String,
    size: String,
    uploaded: {type:Date, default:(Date.now)},
    expires: {type:Date, default:(Date.now()+oneDay)}
});
var Model = mongoose.model("Model", fileSchema);

我的查询在这里:

Model.find({},function(err, file) {
    console.log(err)
    console.log(file);  
});

我可以将内容上传到数据库并通过 RockMongo 查看它们,但之后我无法获取它们.这是我第一次使用 MongoDB,所以我想我只是缺少一些基础知识.任何朝正确方向的推动都会很棒!

I can upload things to the database and see them via RockMongo but I cannot fetch them after. This my first time using MongoDB so I think I'm just missing some of the fundamentals. Any push in the right direction would be great!

推荐答案

mongoose.model 的调用建立了模型所关联的集合的名称,默认值为 复数,小写的模型名称.因此,对于您的代码,这将是 'models'.要将模型与 files 集合一起使用,请将该行更改为:

The call to mongoose.model establishes the name of the collection the model is tied to, with the default being the pluralized, lower-cased model name. So with your code, that would be 'models'. To use the model with the files collection, change that line to:

var Model = mongoose.model("Model", fileSchema, "files");

var Model = mongoose.model("file", fileSchema);

这篇关于Mongoose 总是返回一个空数组 NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆