根据唯一的id mongodb选择完整记录

时间:2016-10-24 18:17:05

标签: mongodb distinct aggregation-framework

我的集合中有一个唯一的 public static void SetupMobileApp(IAppBuilder app, IKernel kernel) { HttpConfiguration config = new HttpConfiguration(); //new MobileAppConfiguration() // .UseDefaultConfiguration() // .ApplyTo(config); new MobileAppConfiguration() .MapApiControllers() .AddTables(new MobileAppTableConfiguration() .MapTableControllers() .AddEntityFramework()) .AddPushNotifications() .MapLegacyCrossDomainController() .ApplyTo(config); 字段,但_id字段中有重复的值。我想用唯一的transactionreceipt过滤掉它,我想要该记录的完整文档数据..这可能吗?

我读到只有索引可以从使用distinct获取..有什么地方我也可以获得完整的文档。?

我的数据集是:

transactionreceipt

查询就像

{"_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 } ) ,没有别的。我哪里错了?有没有可以做到这一点的聚合函数?

1 个答案:

答案 0 :(得分:0)

如何尝试以下方式 -

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/