使用mongoDb过滤时间戳

时间:2013-06-26 14:22:10

标签: python mongodb

假设这个Json从我的mongoDb导出

{
   "_id":{
      "$oid":"51ca002d9e67460354bb0089"
   },
   "node":{
      "id":"1",
      "components":[
         {
            "sensors":[
               {
                  "name":"backup-job-name",
                  "value":"Job_2"
               },
               {
                  "name":"backup-job-id",
                  "value":"187"
               },
               {
                  "name":"backup-start-date",
                  "value":"1372138227"
               },
               {
                  "name":"backup-stop-date",
                  "value":"1372138235"
               },
               {
                  "name":"backup-nb-errors",
                  "value":"0"
               },
               {
                  "name":"backup-nb-warnings",
                  "value":"0"
               },
               {
                  "name":"backup-source-files",
                  "value":"402"
               },
               {
                  "name":"backup-source-bytes",
                  "value":"168571449"
               },
               {
                  "name":"backup-status",
                  "value":null
               }
            ],
            "type":"backup",
            "serial":"1-backup-backup-job-name-Job_2"
         },
         {
            "sensors":[
               {
                  "name":"backup-job-name",
                  "value":"Test_bckp"
               },
               {
                  "name":"backup-job-id",
                  "value":""
               },
               {
                  "name":"backup-start-date",
                  "value":"0"
               },
               {
                  "name":"backup-stop-date",
                  "value":"0"
               },
               {
                  "name":"backup-nb-errors",
                  "value":"0"
               },
               {
                  "name":"backup-nb-warnings",
                  "value":"0"
               },
               {
                  "name":"backup-source-files",
                  "value":"0"
               },
               {
                  "name":"backup-source-bytes",
                  "value":"0"
               },
               {
                  "name":"backup-status",
                  "value":null
               }
            ],
            "type":"backup",
            "serial":"1-backup-backup-job-name-Test_bckp"
         }
      ]
   },
   "timestamp":1372192813
}

我使用python,我想获取“backup-start-date”(存储为时间戳)il大于给定值的文档。

我试过

collection.find({
        'node.components.type': 'backup', 
        'node.components.sensors': {'name': 'backup-start-date', 'value': {'$gte': ts_to_compare_with}}}):

但结果中的任何文件。我的查询错了吗?

1 个答案:

答案 0 :(得分:1)

有两件事 - 您希望匹配组件数组中的传感器文档,而$gte仅适用于整数和日期。

如果您将数据转换为整数,那么您可以使用$gte并进行查询,如下所示:

db.test.find({
  'node.components.type': 'backup', 
  'node.components.sensors': { 
      $elemMatch: {'name': 'backup-start-date', 
                   'value': {'$gte': 168571445}}}})