无法在MongoDb中比较时刻日期

时间:2019-01-02 20:41:54

标签: javascript mongodb date mongoose momentjs

所以我的目标是检索带有猫鼬今天发表的评论的帖子。

首先,我使用以下命令创建一个开始的UTC当前日期对象:

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

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

然后,我想用猫鼬创建数据库搜索,以获取今天发表评论的帖子

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 } }
    ]

})

第三,我的猫鼬注释文档具有created_date属性

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

一切看起来都不错,但是由于某些原因,数据库搜索后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;

编辑2:示例发布文档

{ _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 个答案:

答案 0 :(得分:3)

因此,这里的问题是您有两个集合postscomments。根据{{​​1}}模式,Posts数组仅包含引用存储在第二个集合中的文档的ID。因此,您可以检查该数组是否存在并且不为空,但是不能直接引用这些元素。

要解决此问题,可以使用$lookup将这些文档从comments放入comments,然后可以在$match内应用日期条件,请尝试:

posts