2017-12-08 11 views
0

私は、ここにいくつかの例のデータであり、pythonで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

私のスクリプトはあなたを助けましたか? –