2016-02-25 3 views
0

Как выполнить этот запрос в мангусте я нашел несколько примеров, но я не смог найти решение этойнайти все данные с группой по в мангусте

select * from table group by name 

Я попробовал этот

table.aggregate([ 
     { 
      $group: { 
       _id: '$name',     
      } 
     } 
    ], function (err, d) { 
     if (err) { 
      console.log(err); 
     } else { 
      console.log(JSON.stringify(d)); 
     } 
    }); 

Но он показывает только имя не все данные

ответ

0

Вы можете использовать что-то вроде этого он должен работать

table.aggregate([ 
     { 
      $group: { 
       _id: '$name', //$region is the column name in collection 
       count: {$sum: 1} 
      } 
     } 
    ], function (err, result) { 
     if (err) { 
      next(err); 
     } else { 
      res.json(result); 
     } 
    }); 
0

данные

> db.tt.find({}) 
{ "_id" : ObjectId("56a96dc8081f2fc07ecd7ffb"), "name" : "test", "numbers" : [ 7 
6.3922, 42.9196154, ObjectId("56aea43cb6be380000616b07"), 789.11 ] } 
{ "_id" : ObjectId("56a96dee081f2fc07ecd7ffc"), "name" : "test", "numbers" : [ 8 
7.938547, 42.9196154 ] } 
{ "_id" : ObjectId("56a96e00081f2fc07ecd7ffd"), "name" : "test", "numbers" : [ 4 
2.9196154, 87.938547 ] } 

$group содержит другое поле с <fieldName>: '$fieldName', как показано ниже.

> db.tt.aggregate([{$group: {_id: {name: '$name', numbers: '$numbers'}}}]) 

Результаты

{ "_id" : { "name" : "test", "numbers" : [ 76.3922, 42.9196154, ObjectId("56aea4 
3cb6be380000616b07"), 789.11 ] } } 
{ "_id" : { "name" : "test", "numbers" : [ 87.938547, 42.9196154 ] } } 
{ "_id" : { "name" : "test", "numbers" : [ 42.9196154, 87.938547 ] } } 
Смежные вопросы