簡體   English   中英

Mongoose / mongoDb 搜索我需要未填充屬性的值

[英]Mongoose / mongoDb search where I need values of unpopulated property

所以我正在用 mongodb / mongoose 進行搜索。 我有一個 post 文檔,這個 post 文檔有一個 comments 屬性,它由 comment IDS 組成。 此搜索的目標是收集今天添加了評論的所有用戶帖子。

我找不到任何帖子,因為我正在搜索 comments.created_date 並且此屬性尚不存在,因為我在 comments 屬性的數組中只有評論 IDS。

我如何“填充”這個數組,以便我可以找到添加了評論的帖子?

代碼:首先我們生成這一天開始的Date對象。 然后我們搜索帖子。 在這里,我談論的是第一個 $or 案例:comments.created_date 不起作用,因為此屬性由評論 ID 組成。 如何獲取用戶今天發表評論的帖子?

 // Generate the actual time
    const todayForEvent = moment().startOf('day')
      .utc().toDate();



    const posts = await Post.find({

        $or: [{
        // From this user...
        $and: [
          // Find normal posts that has comments (recent interactions)
          { _posted_by: userId },
          { comments: { $exists: true, $ne: [] } },
          { 'comments.created_date': { $gte: todayForEvent } }
        ]
      }, {
        $and: [
          // Find tasks due to today
          { 'task._assigned_to': userId },
          { 'task.due_to': { $in: [today, tomorrow] } }
        ]
      }, {
        $and: [
          // Find events due to today
          { 'event._assigned_to': userId },
          { 'event.due_to': { $gte: todayForEvent, $lt: todayPlus48ForEvent } }
        ]
      }]
    })
      .sort('event.due_to task.due_to -comments.created_date')
      .populate('_posted_by', 'first_name last_name profile_pic')
      .populate('task._assigned_to', 'first_name last_name')
      .populate('event._assigned_to', 'first_name last_name')
      .populate('_group', 'group_name group_avatar')
      .populate('_liked_by', 'first_name last_name');

    console.log('POSTS', posts);

模特兒

const mongoose = require('mongoose');

const { Schema } = mongoose;

const PostSchema = new Schema({
  content: {
    type: String,
    trim: true
  },
  _content_mentions: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }],
  type: {
    type: String,
    required: true,
    enum: ['normal', 'event', 'task']
  },
  _liked_by: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }],
  comments_count: {
    type: Number,
    default: 0
  },
  comments: [{
    type: Schema.Types.ObjectId,
    ref: 'Comment'
  }],
  _group: {
    type: Schema.Types.ObjectId,
    ref: 'Group',
    required: true
  },
  _posted_by: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  task: {
    due_to: {
      type: String,
      default: null
    },
    _assigned_to: {
      type: Schema.Types.ObjectId,
      ref: 'User'
    },
    status: {
      type: String,
      enum: ['to do', 'in progress', 'done']
    }
  },
  event: {
    due_to: {
      type: Date,
      default: null
    },
    _assigned_to: [{
      type: Schema.Types.ObjectId,
      ref: 'User'
    }]
  },
  created_date: {
    type: Date,
    default: Date.now
  },
  files: [{
    orignal_name: {
      type: String,
      default: null
    },
    modified_name: {
      type: String,
      default: null
    }
  }]
});

const Post = mongoose.model('Post', PostSchema);

module.exports = Post;

評論模型

const mongoose = require('mongoose');
const moment = require('moment');

const { Schema } = mongoose;

const CommentSchema = new Schema({
  content: {
    type: String,
    trim: true
  },
  _content_mentions: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }],
  created_date: {
    type: Date,
    default: moment().utc().toDate()
  },
  _commented_by: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  _post: {
    type: Schema.Types.ObjectId,
    ref: 'Post',
    required: true
  }
});

const Comment = mongoose.model('Comment', CommentSchema);

module.exports = Comment;

由於您必須使用 comments created_date字段過濾文檔,因此您必須使用 comment 集合而不是 post 開始聚合,並使用$lookup聚合加入posts

const todayForEvent = moment().startOf('day').utc().toDate()

db.comments.aggregate([
  { "$match": { "created_date": { "$gte": todayForEvent } }},
  { "$lookup": {
    "from": "posts",
    "localField": "_id",
    "foreignField": "comments",
    "as": "posts"
  }},
  { "$unwind": "$posts" },
  { "$replaceRoot": { "newRoot": "$posts" }}
])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM