1

假设这个 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,我想获取“备份开始日期”(存储为时间戳)大于给定值的文档。

我试过了

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

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

4

1 回答 1

2

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

如果您将数据转换为整数,那么您可以$gte像这样使用和查询:

db.test.find({
  'node.components.type': 'backup', 
  'node.components.sensors': { 
      $elemMatch: {'name': 'backup-start-date', 
                   'value': {'$gte': 168571445}}}})
于 2013-06-26T14:44:29.860 回答