所以我有這個貓鼬架構:var mongoose = require('mongoose');var Schema = mongoose.Schema;var CommentSchema = new Schema({ body: {type: String, required: true, max: 2000}, created: { type: Date, default: Date.now }, flags: {type: Number, default: 0}, lastFlag: {type: Date, default: Date.now()}, imageBanned: {type: Boolean, default: false}, fileName: {type: String, default: ""}}, { writeConcern: { w: 0, j: false, wtimeout: 200 } });var PostSchema = new Schema({ body: {type: String, required: true, max: 2000}, created: { type: Date, default: Date.now }, flags: {type: Number, default: 0}, lastFlag: {type: Date, default: Date.now()}, fileName: {type: String, default: ""}, imageBanned: {type: Boolean, default: false}, board: {type: String, default: ""}, comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]}, { writeConcern: { w: 0, j: false, wtimeout: 200 } });var Post = mongoose.model('Post', PostSchema);var Comment = mongoose.model('Comment', CommentSchema)module.exports = {Post, Comment}我正在嘗試在帖子的評論數組中查詢評論。這是我正在嘗試的端點:router.post('/flagComment', (req, res, next)=>{ console.log('inside /flagComment') console.log('value of req.body: ', req.body) model.Post.findOne({"comments._id": req.body.id}).exec((err, doc)=>{ if(err){ console.log('there was an error: ', err) } console.log('the value of the found doc: ', doc) res.json({dummy: 'dummy'}) })})但是,這提供了以下終端輸出:value of req.body: { id: '5c9bd902bda8d371d5c808dc' }the value of the found doc: null那是不正確的...我已經驗證了ID是正確的-為什么找不到注釋文檔?
Mongodb無法按ID查詢子文檔(返回null)
慕村9548890
2021-03-29 17:15:30