使用java在mongodb中使用$ project检索列?

时间:2017-03-19 10:42:36

标签: java mongodb aggregation-framework mongodb-java

db.test.aggregate( {$project : { ID : 1, amount : 1, NAME : 1}})

给我结果:

{ "_id" : ObjectId("58cbbc909b76f65dd4e6244e"), "ID" : 1, "NAME" : "dgsdg", "amount" : 600}
{ "_id" : ObjectId("58cbbc909b76f65dd4e6244f"), "ID" : 2, "NAME" : "fdfd", "amount" : 200}
{ "_id" : ObjectId("58cbbc909b76f65dd4e62450"), "ID" : 3, "NAME" : "jfhjd", "amount" : 300}
{ "_id" : ObjectId("58cbbc909b76f65dd4e62451"), "ID" : 4, "NAME" : "kjhfjh", "amount" : 400}
{ "_id" : ObjectId("58cbbc909b76f65dd4e62452"), "ID" : 5, "NAME" : "kjjdj", "amount" : 500}
{ "_id" : ObjectId("58cbbc909b76f65dd4e62453"), "ID" : 6, "NAME" : "fdfd", "amount" : 300}

当我在java中执行此操作时:

DBObject project = new BasicDBObject("$project", new BasicDBObject("NAME", 1)
         .append("amount", 1).append("ID", 1));
DBObject group1 = new BasicDBObject("$group", new BasicDBObject("_id", "$NAME"));
        AggregationOutput output = coll.aggregate(project,group1);

给我结果:

{ "_id" : "kjjdj"} 

如何使用上面的java代码实现以下结果:

{ "_id" : ObjectId("58cbbc909b76f65dd4e6244e"), "ID" : 1, "NAME" : "dgsdg", "amount" : 600}

0 个答案:

没有答案