You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从MongoDB文档中获取content数组字段的问题求助

无法获取MongoDB文档中的content数组字段

我正尝试从MongoDB的blog集合中获取文档,目前集合里只有一个手动创建的文档:
数据库中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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:40:13