mongodb删除所有少于指定的日期 [英] mongodb remove all dates less than specified

查看:782
本文介绍了mongodb删除所有少于指定的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据。

{
    deviceID: 186,
    date: "2014-3-15"
}
{
    deviceID: 186,
    date: "2014-3-14"
}
{
    deviceID: 186,
    date: "2014-3-13"
}

以及一些较低的日期,例如 2014-3-9,8,7,6 等。

And some lower dates, like 2014-3-9 , 8 ,7 ,6 etc.

a db.coll.remove({date:{$ lte: 2014-3-5}})

Mongo也删除了 15,14,13 ,但保留了一位数字的日期。

Mongo removes the 15,14,13 aswell, but keeps single digit day dates. Is this maybe due to the date is a string?

我不知道如何格式化日期,以便删除某个日期以下的所有日期。

I dont know how else to format the date so I can remove all dates below a certain date.

这应该是一个清理过程,删除日期小于指定日期的所有文档。

It is supposed to be a cleaning process, removing all documents with a date lower than specified.

推荐答案

这是因为要查询的日期字段是一个字符串字段,而不是Date()。在您的mongo文档中,而不是自定义日期字符串中,将javascript日期对象插入日期字段。

Its because the date field you are querying on is a string filed and not a Date(). In your mongo documents instead of a custom date string, insert javascript date objects into date field.

like

{ deviceID: 186,,"date": new Date(2012, 7, 14) }

,然后执行删除操作,就像

and when you execute the remove do it like

db.coll.remove({date:{$lte:new Date(2012, 7, 14)}})

这篇关于mongodb删除所有少于指定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆