mongodb 删除所有小于指定的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22422178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodb remove all dates less than specified
提问by user3422790
I have the following data.
我有以下数据。
{
deviceID: 186,
date: "2014-3-15"
}
{
deviceID: 186,
date: "2014-3-14"
}
{
deviceID: 186,
date: "2014-3-13"
}
And some lower dates, like 2014-3-9 , 8 ,7 ,6
etc.
还有一些较低的日期,例如2014-3-9 , 8 ,7 ,6
等。
When doing a db.coll.remove({date:{$lte:"2014-3-5"}})
当做一个 db.coll.remove({date:{$lte:"2014-3-5"}})
Mongo removes the 15,14,13
aswell, but keeps single digit day dates. Is this maybe due to the date is a string?
Mongo 删除了同样的内容15,14,13
,但保留了个位数的日期。这可能是由于日期是一个字符串吗?
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.
这应该是一个清理过程,删除所有日期低于指定日期的文档。
回答by mithunsatheesh
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.
这是因为您查询的日期字段是一个字符串,而不是 Date()。在您的 mongo 文档而不是自定义日期字符串中,将 javascript 日期对象插入日期字段。
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)}})
回答by Abdul Rahman A Samad
If you want to remove data from MongoDB from the date less than specified, you MUST make sure of the date.
如果要从 MongoDB 中删除小于指定日期的数据,则必须确保该日期。
Easiest way for you to check whether you are inputting the right format is to test it before you use it in your query.
检查您输入的格式是否正确的最简单方法是在将其用于查询之前对其进行测试。
For example if you want to get current date in ISODate in Mongo shell, just type new Date
and you will get the current date in Mongo.
例如,如果您想在 Mongo shell 中获取 ISODate 中的当前日期,只需键入new Date
,您将在 Mongo 中获取当前日期。
I've tried the following in the Mongo shell:
我在 Mongo shell 中尝试了以下操作:
new Date(2017, 11, 1)
and it returns
它返回
ISODate("2017-11-30T16:00:00Z")
which is not what I wanted.
这不是我想要的。
What I want is to delete data before 1 November 2017.
我想要的是在 2017 年 11 月 1 日之前删除数据。
Here's what works for me:
以下是对我有用的内容:
new Date("2017-11-01")
and it returns:
它返回:
ISODate("2017-11-01T00:00:00Z")
Which is what I wanted.
这就是我想要的。
回答by Salvador Dali
This is because you are storing your data in a wrong format. You have a string an string
'15'
is smaller than string '5'
. Convert your strings in the beginning to date (read herehow to use dates in mongo).
这是因为您以错误的格式存储数据。你有一个 string 一个 string
'15'
比 string 小'5'
。将开头的字符串转换为日期(在此处阅读如何在 mongo 中使用日期)。
And only than you can use it to properly compare your dates:
并且只有您可以使用它来正确比较您的日期:
db.coll.remove({
date:{
$lte : new Date(2012, 7, 14)
}
})
回答by Neil Lunn
The reason for this is is your dates are strings.
原因是你的日期是字符串。
So in a lexical sense when comparing strings "2014-3-5" isgreater than "2014-3-15", as what is being compared is that "1" is less than "5".
因此,在一个词汇意义比较字符串时“2014年3月5日”是不是“2014年3月15日”更大,因为什么被比较是,“1”小于“5”。
Fix your dates as real ISO Dates, or you will forever have this problem.
将您的日期修正为真正的 ISO 日期,否则您将永远遇到这个问题。
Batch convert like this, assuming "year" "month" "day" in format:
像这样批量转换,假设格式为“年”“月”“日”:
db.eval(function(){
db.collection.find().forEach(function(doc) {
var d = doc.date.split("-");
var date = new Date(
"" + d[0] + "-" +
( d[1] <= 9 ) ? "0" + d[1] : d[1] + "-" +
( d[2] <= 9 ) ? "0" + d[2] : d[2]
);
db.collection.update(
{ "_id": doc._id },
{ "$set": { "date": date }
);
});
})
That makes sure you get the right dates on conversion.
这可确保您获得正确的转换日期。