一、分組查詢
? ? ? ? 在mongoDB中可以使用aggregate中的$group操作對集合中的文檔進行分組,但是查詢后的數據不顯示其他字段,只顯示分組字段
?aggregate進行分組示例
db.collection.aggregate([{$group: {_id: "$field"}},])
查詢后顯示
展開只顯示兩個字段
二、顯示所有字段
2.1 $first顯示
? ? ? ? 顯示字段示例
db.collection.aggregate([{$group: {_id: "$field"},field:{$first:'$field'}},])
? ? ? ? 查詢后字段顯示
2.3 $project顯示
?
db.collection.aggregate([{$group: {_id: "$field"},field:{$first:'$field'}},{$project:{_id:0,'field':'$_id.field'}},])
查詢顯示想要字段