我正在为现有的mongodb Atlas集群编写一个Node.js后端。
当我跑步时,我的路线
const routes = (app) => {
app.route('/file')
.get((req, res, next) => {
console.log('request from : ' + req.originalUrl)
console.log('request type : ' + req.method)
next();
}, getFiles);
}其中getFiles如下所示:
const ChronicFile = mongoose.model('File', FileSchema);
export const getFiles = (req, res) => {
ChronicFile.find({}, (err, chronicFile) => {
if (err) {
res.send(err);
}
console.log(chronicFile);
res.json(chronicFile);
});
}我得到一个返回的空数组:
ur server running on port4000
request from : /file
request type : GET
[]根据mongodb shell的数据库结构是标题为Chronic的整体数据库和三个标题为Chronic Files、fs.chunks和fs.files的集合。我的目标是查询fs.files集合并从中获取所有内容。
fs.files集合中的方案如下:
{
"_id":{
"$oid":"604e57219ffdaa7e11a8edad"
},
"length":{
"$numberLong":"3163108"
},
"chunkSize":261120,
"uploadDate":{
"$date":"2021-03-14T18:34:10.586Z"
},
"filename":"4-dance-a-complex.mp3",
"metadata":{
}
}我的mongoose模式如下:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
export const FileSchema = new Schema({
id: {
type: mongoose.Types.ObjectId
},
chunkSize : {
type: Number
},
fileName : {
type: String
},
length: {
type: Number
},
metadata : {
type: Object
},
uploadDate : {
type: Date
}
}, { collection : 'Chronic.fs.files'});我如上所示添加了集合名称,因为我查看了之前的stackoverflow帖子,发现它们存在命名问题。是我做错了什么,还是我遗漏了什么?当我在mongo shell上执行db.getName()时,它会显示Chronic;当我执行db.fs.files.find()时,它会输出该集合中的所有条目。
发布于 2021-03-31 00:24:53
答案就在下面这段代码中:
const ChronicFile = mongoose.model('File', FileSchema);
export const getFiles = (req, res) => {
ChronicFile.find({}, (err, chronicFile) => {
if (err) {
res.send(err);
}
console.log(chronicFile);
res.json(chronicFile);
});
}我需要将实际的集合名称传递给mongoose模型,而不仅仅是File
https://stackoverflow.com/questions/66873886
复制相似问题