2015-07-05 117 views
2

我的文檔結構如下所示:總平均

{ 
    "_id" : ObjectId("52517d9e6e0af435ddd48219"), 
    "date" : ISODate("2013-10-06T18:11:26.329Z"), 
    "engines" : { 
     "ahnlab" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "ahnlab", 
      "node_name" : "", 
      "status" : 1, 
      "task_id" : "7fac4f67-2bde-49de-980a-4f7fa1d46db7", 
      "threat" : "EICAR_Test_File" 
     }, 
     "avast" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "avast", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "fdce50e9-9bf3-4cc9-91a4-b674a108d478", 
      "threat" : "" 
     }, 
     "avg" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "avg", 
      "node_name" : "", 
      "status" : 1, 
      "task_id" : "4bdb4a37-80ab-4631-8587-edcbde7e2592", 
      "threat" : "EICAR_Test" 
     }, 
     "avira" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "avira", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "43b43bab-ff67-440c-9919-f6241ccaf539", 
      "threat" : "" 
     }, 
     "bitdefender" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "bitdefender", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "ba7efe74-ef93-465f-b6c3-6c4fc746934d", 
      "threat" : "" 
     }, 
     "comodo" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "comodo", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "831d2dc8-704b-4eb7-9a75-9a0364a8ab09", 
      "threat" : "" 
     }, 
     "drweb" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "drweb", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "ebb758c3-9146-4a99-b36f-0fb6ee024a33", 
      "threat" : "" 
     }, 
     "f-prot" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "f-prot", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "13590a25-ac0d-4b1b-b93e-bc715009432a", 
      "threat" : "" 
     }, 
     "forticlient" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "forticlient", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "", 
      "threat" : "" 
     }, 
     "kaspersky" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "kaspersky", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "", 
      "threat" : "" 
     }, 
     "malwarebytes" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "malwarebytes", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "a97f3d9a-d6a9-44df-8355-c053e9f4980a", 
      "threat" : "" 
     }, 
     "mcafee" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "mcafee", 
      "node_name" : "", 
      "status" : 1, 
      "task_id" : "e7a592bb-84fc-4c47-a1ed-3719874b19cc", 
      "threat" : "EICAR test file" 
     }, 
     "msessentials" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "msessentials", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "01c9fb71-155a-473d-b45c-91fa117ae649", 
      "threat" : "" 
     }, 
     "nod32" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "nod32", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "a715cc8f-0e2f-4698-b883-a35fa6add13e", 
      "threat" : "" 
     }, 
     "norman" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "norman", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "29da2955-0674-45d2-ac4d-c0b3ea401cba", 
      "threat" : "" 
     }, 
     "norton" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "norton", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "c4cd1e3e-2f0d-4bf6-84cf-ab0962f7f4ed", 
      "threat" : "" 
     }, 
     "panda" : { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "panda", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "b0888f7c-e4a4-4b4e-b163-21d283e166f1", 
      "threat" : "" 
     } 
    }, 
    "expiration_date" : ISODate("2013-10-06T18:11:36.329Z"), 
    "file_name" : "ffgtr.exe", 
    "scan_status" : "DONE", 
    "task_id" : "4ce4ae9e-ef0a-476a-8189-92a5bfe328bd" 
} 

我想與聚集的領域「日期」和每一個引擎的「定義」項目。 (「定義」是一個嵌入式領域)

我成功做它只是如果我指定特定的引擎:

cursor = collection.aggregate([ 
    { "$match": { "date": { "$gte": startdate } } }, 
    {'$project': { "def": "$engines.avast.definitions","date":1,"_id": 0 }} 
], allowDiskUse=True) 

但我想沒有指定特定的引擎運行所有引擎查詢,東西像:

cursor = collection.aggregate([ 
    { "$match": { "date": { "$gte": startdate } } }, 
    {'$project': { "def": "$engines.$elemMatch.definitions","date":1,"_id": 0 }} 
], allowDiskUse=True) 

(不工作)

我想找到的是「日期」,每個「E之間的平均差異ngine「定義每個引擎類型的日期。

+1

它不會那樣工作。您應該將引擎定義爲「數組/列表」,其中每個「定義」值具有「一致」路徑。聚合框架,就像其他MongoDB查詢運算符一樣,不能像這樣遍歷對象結構。目前結構的唯一方法是使用mapReduce。但是,除此之外,你實際上並沒有說明你正在試圖做什麼。 –

回答

2

這裏的主要問題是數據的結構。爲了與聚合框架一起工作,並且還樂意與大多數其他MongoDB查詢操作和索引實踐一起工作,那麼您的數據應該像這樣結構化。

{ 
    "_id" : ObjectId("52517d9e6e0af435ddd48219"), 
    "date" : ISODate("2013-10-06T18:11:26.329Z"), 
    "engines" : [ 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "ahnlab", 
      "node_name" : "", 
      "status" : 1, 
      "task_id" : "7fac4f67-2bde-49de-980a-4f7fa1d46db7", 
      "threat" : "EICAR_Test_File" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "avast", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "fdce50e9-9bf3-4cc9-91a4-b674a108d478", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "avg", 
      "node_name" : "", 
      "status" : 1, 
      "task_id" : "4bdb4a37-80ab-4631-8587-edcbde7e2592", 
      "threat" : "EICAR_Test" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "avira", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "43b43bab-ff67-440c-9919-f6241ccaf539", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "bitdefender", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "ba7efe74-ef93-465f-b6c3-6c4fc746934d", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "comodo", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "831d2dc8-704b-4eb7-9a75-9a0364a8ab09", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "drweb", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "ebb758c3-9146-4a99-b36f-0fb6ee024a33", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "f-prot", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "13590a25-ac0d-4b1b-b93e-bc715009432a", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "forticlient", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "kaspersky", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "malwarebytes", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "a97f3d9a-d6a9-44df-8355-c053e9f4980a", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "mcafee", 
      "node_name" : "", 
      "status" : 1, 
      "task_id" : "e7a592bb-84fc-4c47-a1ed-3719874b19cc", 
      "threat" : "EICAR test file" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "msessentials", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "01c9fb71-155a-473d-b45c-91fa117ae649", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "nod32", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "a715cc8f-0e2f-4698-b883-a35fa6add13e", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "norman", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "29da2955-0674-45d2-ac4d-c0b3ea401cba", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "norton", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "c4cd1e3e-2f0d-4bf6-84cf-ab0962f7f4ed", 
      "threat" : "" 
     }, 
     { 
      "definitions" : ISODate("2000-01-01T00:00:00.000Z"), 
      "name" : "panda", 
      "node_name" : "", 
      "status" : 4, 
      "task_id" : "b0888f7c-e4a4-4b4e-b163-21d283e166f1", 
      "threat" : "" 
     } 
    ], 
    "expiration_date" : ISODate("2013-10-06T18:11:36.329Z"), 
    "file_name" : "ffgtr.exe", 
    "scan_status" : "DONE", 
    "task_id" : "4ce4ae9e-ef0a-476a-8189-92a5bfe328bd" 
} 

然後與結構的工作是處理$unwind,然後你的結果$group簡單:

db.collection.aggregate([ 
    { "$unwind": "$engines" }, 
    { "$group": { 
     "_id": "$engines.name", 
     "avgInterval": { "$avg": { 
      "$subtract": [ "$date", "$engines.definitions" ] 
     }} 
    }} 
]) 

的差異將在兩個日期對象之間的毫秒。

否則你被卡住遍歷使用JavaScript與MapReduce的代碼中的對象鍵:

db.collection.mapReduce(
    function() { 
     var doc = this; 
     Object.keys(doc.engines).forEach(function(key) { 
      emit(key, 
       (doc.date.valueOf() - 
        doc.engines[key].definitions.valueOf()) 
      ); 
     }) 
    }, 
    function(key,values) { 
     return (Array.sum(values)/values.length); 
    }, 
    { "out": { "inline": 1 } } 
); 

都將給您爲每個引擎「名」翻過集合或其他查詢輸入的平均值。

嘗試改變結構,因爲它更適合您的持續需求,並提供更快的處理。