在MongoDB中顯示來自另一個(gè)集合的數(shù)據(jù)

我在MongoDB中有兩個(gè)單獨(dú)的集合1->帖子,2->評(píng)論。

Post-schema:

const postSchema = new mongoose.Schema(
  {
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    media: {
      type: [mongoose.Schema.Types.Mixed],
      trim: true,
      required: true,
    },
    text: String,
    mentions: {
      type: Array,
      default: [],
    },
    hashTags: {
      type: ["String"],
    },
    likes: {
      type: Array,
      default: [],
    },
    postStatus: {
      type: "String",
      default: "public",
      enum: ["public", "private"],
    },
    deletedAt: {
      type: "Date",
      default: null,
    },
  },
  { timestamps: true }
);

comment schema:

const commentSchema = new mongoose.Schema(
  {
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    postId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Post",
      required: true,
    },
    commentText: String,
  },
  { timestamps: true }
);

現(xiàn)在我想顯示請(qǐng)求帖子的所有評(píng)論。Post表沒有任何鏈接到注釋的內(nèi)容,這就是為什么我不能使用populate()。但評(píng)論表已發(fā)布連接。

以下是我嘗試過的:

exports.getPostById = async (req, res) => {
  try {
    let post = await Post.findById(req.params.id);
    if (!post) return res.status(404).json({ message: "No Post found" });
    let comment = await Comment.find({ postId: { $in: {post: req.params.id} } });//Wrong query
    return res.status(200).send(post);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};
? 最佳回答:
主站蜘蛛池模板: 国产在线一区二区三区在线| 国产精品视频一区二区猎奇| 人妻体内射精一区二区三区| 亚洲一区电影在线观看| 色婷婷av一区二区三区仙踪林| 亚洲国产精品一区二区久久hs| 日韩精品一区二区三区中文字幕| 91无码人妻精品一区二区三区L| 成人免费av一区二区三区| 国产成人无码精品一区不卡| 亚洲av无码一区二区三区在线播放| 成人无码AV一区二区| 伦理一区二区三区| 亚洲美女高清一区二区三区| 狠狠爱无码一区二区三区| 日本精品一区二区三本中文| 日韩成人无码一区二区三区| 国产乱码精品一区二区三区香蕉 | 国产精品成人一区二区三区| 中文字幕在线观看一区二区三区| 国产91久久精品一区二区| www一区二区三区| 精品一区高潮喷吹在线播放| 国产精品538一区二区在线| 亚洲AV成人一区二区三区AV | 福利视频一区二区牛牛| 亚洲AV综合色区无码一区| 国产精品视频一区| 无码人妻AV免费一区二区三区| 色婷婷亚洲一区二区三区| 久久99热狠狠色精品一区| 亚洲欧美日韩国产精品一区| 久久精品国产亚洲一区二区三区 | 精品视频午夜一区二区| 激情内射亚洲一区二区三区| 国产日韩高清一区二区三区| 亚洲AV无码一区二区三区鸳鸯影院| 国产精品伦一区二区三级视频| 一区二区网站在线观看| 欧美激情一区二区三区成人| 色噜噜狠狠一区二区三区果冻|