mongo查询列字段别名
2018-05-03 本文已影响0人
似水之星
在mongo中实现类似"select name as new name from use"的mysql的列别名的效果。
shell实现
db.getCollection('Exchange').aggregate({$project:{allowed:"$isAllowed"}})
isAllowed是mongo的Exchange表中的真实列名,allowed是别名。
在mongo中不用别名查询效果如下:
db.getCollection('Exchange').aggregate({}})

在mongo中用别名查询效果如下:
db.getCollection('Exchange').aggregate({$project:{allowed:"$isAllowed"}})

java实现
List<DBObject> pipeline = new ArrayList<>();
DBObject projectFields = new BasicDBObject();
#pmOpen是openTimePM列的别名
projectFields.put("pmOpen", "$openTimePM");
DBObject project = new BasicDBObject("$project", projectFields);
pipeline.add(project);
AggregationOutput out = MongoDB.getMongoDB().getCollection("Exchange").aggregate(pipeline);