2017-12-08 4 views
0

내가, 여기에 몇 가지 예를 들어 데이터입니다 파이썬에서 MongoDB를 수집에서 일부 데이터를 선택하기 위해 노력하고있어 :MongoDB를 집계 필터

[{"id":1, "planned_timestamp":1512728425, "executed_timestamp":0, "owner":1, "action_type": "read", "action_params":"book A"}, 
{"id":2, "planned_timestamp":1512728430, "executed_timestamp":0, "owner":1, "action_type": "read", "action_params":"book B"}, 
{"id":3, "planned_timestamp":1512728435, "executed_timestamp":0, "owner":2, "action_type": "read", "action_params":"book C"}] 

내가 타임 스탬프 변수보다는 "executed_timestamp":0, "planned_timestamp" 낮은 수있는 모든 작업을 선택합니다 pymongo와

[{"owner":1, "tasks": [{"id":1,"planned_timestamp":1512728425,"action_type": "read","action_params":"book A"},{"id":1,"planned_timestamp":1512728430,"action_type": "read","action_params":"book B"}]}, 
{"owner":2, "tasks": [{"id":3,"planned_timestamp":1512728435,"action_type": "read","action_params":"book C"}]}] 

내 현재 요청은 다음과 같습니다 :

r = db.task_queue.aggregate(
     [ 
     { "$group" : { "_id" : "$agent_id", "tasks": { "$push": "$$ROOT" } } } 
     ] 
    ) 
나는 다음과 같은 결과를 가지고 싶습니다

답변

0

agent_id은 무엇입니까?

$match이 필요합니다. 이 시도.

timestamp = 1500000000 

r = db.task_queue.aggregate(
     [ 
      {"$match": {"$and": [ 
       { 
        "executed_timestamp": {"$eq": 0}, 
        "planned_timestamp": {"$lte": timestamp} 
       } 
      ]}}, 
      {"$group" : { "_id" : "$owner", "tasks": { "$push": "$$ROOT" } } }, 
      {"$project": 
      { 
       "_id": 0, 
       "owner": "$_id.owner", 
       "tasks": 1 
      }} 
     ] 
    ) 
+0

agent_id는 소유자와 동일합니다. – BadTigrou

+0

내 스크립트가 도움이 되었습니까? –