【问题标题】:Mongo and Mongoose - .find() returns different valuesMongodb 和 Mongoose - .find() 返回不同的值
【发布时间】:2021-08-13 19:16:09
【问题描述】:

难倒这里,应该是非常简单的事情。

我有一个 MERN 堆栈应用程序,它没有按预期从 mongo 中找到数据。

我从前端发布并更新文档。我登录到 Mongo CLI,我可以看到我保存的数据。

但是对于 Node 应用程序,Mongoose 不会返回完整的文档。

这是我获取文档的途径——我什至尝试测试获取 Everything。

router.get("/", async (req, res) => {
  const user_email = req.query.user_email;
  const course_id = req.query.course_id;
  const test = await CourseProgressSchema.find({
    _id: "60acfd1c969cac0bd3a213a8",
  });
  console.log(test);
  try {
    let course_progress;
    if (user_email && course_id) {
      course_progress = await CourseProgressSchema.findOne({
        user_email,
        course_id,
      });
      if (!course_progress) {
        const newCourseProgress = new CourseProgressSchema({
          user_email,
          course_id,
        });
        course_progress = await newCourseProgress.save();
      }
    } else if (user_email && !course_id) {
      course_progress = await CourseProgressSchema.find({ user_email });
    } else if (course_id && !user_email)
      course_progress = await CourseProgressSchema.find({ course_id });
    else {
      res.json({ error: "Not Found." });
    }
    console.log(course_progress);
    res.json({ success: course_progress });
  } catch (error) {
    console.log(error);
    res.json({
      error: "Soemthing went wrong when getting current course progress.",
    });
  }
});

course_progress 被安慰/返回为:

[0] [
[0]   {
[0]     _id: 60acfd1c969cac0bd3a213a8,
[0]     user_email: 'nickisyourfan@icloud.com',
[0]     course_id: '60acfcfe969cac0bd3a213a7',
[0]     __v: 0
[0]   }
[0] ]

但如果我访问 mongo cli 并使用 db.courseprogressscchemas.find().pretty() 它会返回更新后的文档:

{
    "_id" : ObjectId("60acfd1c969cac0bd3a213a8"),
    "user_email" : "nickisyourfan@icloud.com",
    "course_id" : "60acfcfe969cac0bd3a213a7",
    "__v" : 0,
    "course_progress" : {
        "60acfca1969cac0bd3a213a5" : {

        }
    }
}

这是我的架构 - 没什么特别的:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const CourseProgressSchema = new Schema({
  user_email: {
    type: String,
    required: true,
  },
  course_progress: {
    type: Object,
    required: true,
    default: {},
  },
  course_id: {
    type: String,
    required: true,
  },
});

module.exports = CourseProgress = mongoose.model(
  "CourseProgressSchema",
  CourseProgressSchema
);

谁能帮我弄清楚为什么猫鼬只返回文件的一部分而不是整个文件?

【问题讨论】:

    标签: mongodb mongoose mern


    【解决方案1】:

    Mongoose 默认不显示空对象。为了获得这些,您必须在创建架构时将minimize 标志设置为false

    const CourseProgressSchema = new Schema(
      {
        user_email: {
          type: String,
          required: true,
        },
        course_progress: {
          type: Object,
          required: true,
          default: {},
        },
        course_id: {
          type: String,
          required: true,
        },
      },
      { minimize: false }
    );
    

    【讨论】:

    • 会做 - 我确定这是我的问题,非常感谢!
    猜你喜欢
    • 2016-05-01
    • 2022-11-27
    • 2022-10-13
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2018-06-09
    相关资源
    最近更新 更多