代码之家  ›  专栏  ›  技术社区  ›  tilly

无法比较MongoDb中的时间日期

  •  2
  • tilly  · 技术社区  · 7 年前

    因此,我的目标是检索今天使用Mongoose发布的带有评论的帖子。

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

    这导致 2019-01-02T06:00:00.000Z

    然后我想用mongoose创建一个DB搜索,以获取今天放置评论的帖子

    const posts = await Post.find({
            // From this user...
            $and: [
              // Find normal posts that has comments (recent interactions)
              { _posted_by: userId },
              { comments: { $exists: true, $ne: [] } },
              { 'comments.created_date': { $gte: todayForEvent } }
        ]
    
    })
    

    const CommentSchema = new Schema({
    
      created_date: {
        type: Date,
        default: moment().utc().toDate()
      }
    
    });
    
    const Comment = mongoose.model('Comment', CommentSchema);
    
    module.exports = Comment;
    

    这是放置注释后的结果文档

    enter image description here

    编辑:应请求添加post架构

    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;
    

    编辑2:示例post文档

    { _id: 5c2d14c30176ac30204809a8,
        task: { due_to: null },
        event: { due_to: null, _assigned_to: [] },
        _content_mentions: [],
        _liked_by: [],
        comments_count: 1,
        comments: [ 5c2d14dc0176ac30204809ab ],
        content: '<p>poging 5 duust</p>',
        type: 'normal',
        _posted_by:
         { _id: 5c292e0e63deb43d9434f664,
           profile_pic: 'default_user.png',
           first_name: 'Jaspet',
           last_name: 'Houthoofd' },
        _group: 5c292db763deb43d9434f660,
        created_date: 2019-01-02T19:45:07.710Z,
        files: [],
        __v: 0,
        liked_by: [] }
    

    **编辑3:示例注释**

    { _content_mentions: [],
      created_date: 2019-01-02T21:10:04.456Z,
      _id: 5c2d28c251f2bd332cdeaf0a,
      content: '<p>hehe</p>',
      _commented_by: 5c292db763deb43d9434f65f,
      _post: 5c2d1dd254ca0429b470f000,
      __v: 0 }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   mickl    7 年前

    posts 和 comments . 基于你的 Posts 评论 数组仅包含引用存储在第二个集合中的文档的ID。这就是为什么可以检查该数组是否存在且不为空,但不能直接引用这些元素。

    修复您可以使用的 $lookup 把那些文件从 评论 帖子 然后你可以在里面应用你的日期条件 $match ,请尝试:

    let posts = await Post.aggregate([
        { $match: { comments: { $exists: true, $ne: [] }, _postedBy: userId } },
        { $lookup: { from: "comments", localField: "comments", foreignField: "_id", as: "comments" } },
        { $match: { 'comments.created_date': { $gte: todayForEvent } } }
    ])