簡體   English   中英

獲取MongoDB中今天之前過期的文檔

[英]Get documents which expired before today in MongoDB

我正在使用MongoDB 2.6 ,我正在創建一個按過期日期過濾文檔的查詢。 我想知道今天之前哪些文件過期了。 我的持久數據代表出版物。

我已經提出了一些問題,但它沒有返回文件。 我知道有很多文件可以滿足這個條件。

我嘗試了兩個查詢,但沒有人工作:

1。

{
"domain.ApplicationCase.fields.ExpireDate": { $gte : {
 $currentDate: {
    lastModified: true,
    "cancellation.date": { $type: "timestamp" }
 }}}    
}

2。

{
"domain.ApplicationCase.fields.ExpireDate": { $gte : new Date()}    
}

部分文件記錄:

{
    "_id" : "1234546",
    "fields" : {
        "Orchestration" : "default",
        "Segmentation" : "PFI",
        "MatchKey" : "1",
        "UserID" : "001"
    },
    "domain" : {
        "ApplicationCase" : {
            "_id" : null,
            "fields" : {
                "ExpireDate" : "2015-11-13T13:47:26Z",
....

怎么了?

如果你想獲得今天之前過期的記錄,那么你應該使用$lte而不是$gte

db.myCollection.find({
  "domain.ApplicationCase.fields.ExpireDate": { $lte : new Date()}    
})

更新:

因此,您的文檔的核心問題是, domain.ApplicationCase.fields.ExpireDate的值不是日期格式,而是簡單的String格式。

因此,您首先需要將它們轉換為日期以便查詢起作用,因為您要將String與Date進行比較。

也許,您可以使用這樣的代碼將字符串轉換為日期:

db.myCollection.find({
    "domain.ApplicationCase.fields.ExpireDate": {$exists: true}
}).snapshot().forEach(function(record) {
    var stringValue = record.domain.ApplicationCase.fields.ExpireDate;

    db.myCollection.update({_id: record._id}, {$set: {
          "domain.ApplicationCase.fields.ExpireDate": ISODate(stringValue)
    }});
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM