2016-10-24 71 views
0

我有一個集合,它具有唯一的_id字段,但在transactionreceipt字段中具有重複值。我想通過獨特的transactionreceipt將其過濾出來,並且我想要該記錄的完整文檔數據。這可能嗎?根據唯一ID選擇完整記錄mongodb

我讀的只有索引可以從使用不同的取得..有什麼我可以得到完整的文件也。

我的數據集:

{"_id": {"$oid": "580af4ce1b407e114sdvsdbf"}, 
    "transactionreceipt":"734416173864583200", 
    "type":"recharge" 
}, 
{"_id": {"$oid": "580af4ce1b407f495sagfdgaed"}, 
    "transactionreceipt":"734416173864583200", 
    "type":"recharge" 
}, 
{"_id": {"$oid": "580af4ce1b407safasfe114bf"}, 
    "transactionreceipt":"734585659418738700", 
    "type":"recharge" 
}, 
{"_id": {"$oid": "580af4ce1b407f4956436sdg"}, 
    "transactionreceipt":"734585659418738702", 
    "type":"recharge" 
}, 
{"_id": {"$oid": "580af4ce1b407f495safasgvd"}, 
    "transactionreceipt":"734723784694562800", 
    "type":"recharge" 
}, 
{"_id": {"$oid": "580af4ce1b407f495325ds"}, 
    "transactionreceipt":"734723784694562808", 
    "type":"recharge" 
} 

和查詢會是什麼樣

var tx = db.usertransactions.distinct("transactionreceipt",{"type" : "recharge"}) 
tx.forEach(statistics=>{ 
     printjson(parseFloat(statistics));// i want all details to be displayed here 
     } 
) 

我只能得到transactionreceipt這裏,沒有別的。我哪裏錯了?有沒有可以做到這一點的任何聚合函數?

回答

0

什麼努力下way-

var tx = db.usertransactions.aggregate(
[ 
    { $match: { type: "recharge" } }, 
    { 
     $group: {    
      _id : { id: { $id: "$oid" }, transactionreceipt: { $transacreceipt: "$transactionreceipt" }} 
     } 
    }, 

]) tx.forEach(statistics=>{ 
     printjson(parseFloat(statistics)); 
    }) 

您可以查詢詳細情況 - https://docs.mongodb.com/manual/reference/operator/aggregation/group/