Mongodb查询子字符串?

我的数据库是这种格式

{
  "_id": ObjectId("5a934e000102030405000002"),
  "date": "01/05/2020",
  "post": "some random post 2",
  "time": "11:07:42 AM",
  "username": "random user 2"
}, 
{
  "_id": ObjectId("5a934e000102030405000002"),
  "date": "01/05/2020",
  "post": "some random post 2",
  "time": "11:07:42 PM",
  "username": "random user 2"
},

我只想查询具有PM字段的时间,因此我的数据应仅返回到该时间

{
  "_id": ObjectId("5a934e000102030405000002"),
  "date": "01/05/2020",
  "post": "some random post 2",
  "time": "11:07:42 PM",
  "username": "random user 2"
},

TIME here is a string and I would like to substring to the PM part but some time TIME is like 7:38:43 PM so it should remove any whitespace also

请帮忙谢谢

评论
  • oqui
    oqui 回复

    One way to do this is to use a simple regex:

    collection.find({ time: { $regex: "PM"} } , function(err,result){ ... })