猫鼬总是返回一个空数组NodeJS [英] Mongoose always returning an empty array NodeJS
问题描述
我尝试使用find
和findOne
,但都没有返回文档. find
返回空数组,而findOne
返回null
.在null
和null
中都是err
.
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
的调用将建立模型所绑定的集合的名称,默认值为
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);
这篇关于猫鼬总是返回一个空数组NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!