胡子哥哥
可以使用别名,不过要用aggregate()进行查询,并且,要把需要显示的名称全部列出来你要将需要显示的列,全部写在$project中db.getCollection('discerns').aggregate([
{
$project: {
'date': '$DateTime'
}
}
]);如果要起别名,对象的键就是别名,值就是实际的数据库字段名(字段名要用$符号开头,否则 mongodb 会将其解析为一个字符串)_id会默认查询出来,如果不想要 _id 查询出来,需要手动设置 _id:0除 _id外,其他字段值不设置,则不会查询出来
长风秋雁
可以的,将投射过的字段在结果中重命名:原始数据:> db.zipcode.find()
{ "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
{ "_id" : "01002", "city" : "AGAWAM", "loc" : [ -72.51565, 42.377017 ], "pop" : 36963, "state" : "MA" }
{ "_id" : "01005", "city" : "AGAWAM", "loc" : [ -72.108354, 42.409698 ], "pop" : 4546, "state" : "MA" }
{ "_id" : "01007", "city" : "BELCHERTOWN", "loc" : [ -72.410953, 42.275103 ], "pop" : 10579, "state" : "MA" }
{ "_id" : "01008", "city" : "BLANDFORD", "loc" : [ -72.936114, 42.182949 ], "pop" : 1240, "state" : "MA" }显示原字段和重命名字段名称:> db.zipcode.aggregate({$project:{myid:"$_id"}})
{ "_id" : "01001", "myid" : "01001" }
{ "_id" : "01002", "myid" : "01002" }
{ "_id" : "01005", "myid" : "01005" }
{ "_id" : "01007", "myid" : "01007" }
{ "_id" : "01008", "myid" : "01008" }只显示重命名字段名称:> db.zipcode.aggregate({$project:{myid:"$_id",_id:0}})
{ "myid" : "01001" }
{ "myid" : "01002" }
{ "myid" : "01005" }
{ "myid" : "01007" }
{ "myid" : "01008" }