2014-04-07 37 views
-2

這如何選擇MongoDB中這如何選擇MongoDB中

select cidade 
from rendaxeducação 
where idheducacao = (
    select max(idheducacao) 
    from rendaxeducacao 
    where idh=2000 
); 
+1

你有沒有嘗試過的東西沒有? – BaptisteL

+0

這種方式的子查詢目前在MongoDB本身是不可能的,你需要做兩個查詢,一個用於MAX結果,另一個用於根據結果進行選擇 – Sammaye

回答

0
db.rendaxeducacao.aggregate([ 
     {$math : {idh : 2000}     // fiter only where idh=2000 
     }, 
     {$group: { _id: 1, 
        max_idheducacao : {$max : $idheducacao} // find max 
      } 
     }, 
     {$project:{       // select cidade 
       _id:0, 
       cidade:1 
      } 
     } 

    ]) 

rendaxeducacao =收集