无法从MongoDB文档中获取content数组字段的问题求助
无法获取MongoDB文档中的content数组字段
我正尝试从MongoDB的blog集合中获取文档,目前集合里只有一个手动创建的文档:
问题:查询返回的文档里完全没有content数组字段,求指导问题出在哪里。
使用的Mongoose Schema
import { Schema, model, models } from "mongoose"; const ContentSchema = new Schema( { _id: { type: Schema.Types.ObjectId, required: true, }, type: { type: String, required: true, }, }, { _id: false }, ); const BlogSchema = new Schema( { _id: { type: Schema.Types.ObjectId, required: true, }, createdAt: { type: Date, default: Date.now, required: true, }, heading: { type: String, required: true, }, paragraph: { type: String, required: true, }, coverImagePath: { type: String, required: true, }, content: { type: [ContentSchema], required: true, }, }, { timestamps: true }, ); const BlogModel = models.blog || model("blog", BlogSchema); export default BlogModel;
查询代码
const blogsCollectionData = (await BlogModel.find({})) as BlogDocType[];
TypeScript类型定义
export type BlogDocType = { _id: ObjectId; createdAt: Date; paragraph: string; heading: string; coverImgPath: string; content: BlogContentDoc[]; }; export type BlogContentDoc = { type: string; _id: ObjectId; };
实际查询结果
[ { createdAt: 1725884791793, _id: new ObjectId('66dbba46cd201551710e0499'), heading: 'Some of my data', coverImagePath: 'public/blogImages/001.png', createdAT: 2000-12-31T19:00:00.000Z, paragraph: 'This is a test paragraph' } ]
我已经尝试让Schema与数据库文档完全匹配,核对过双方的数据类型,但还是无法获取到content数组,求排查问题。
内容的提问来源于stack exchange,提问作者M.Jafar
相关产品推荐
相关产品推荐

