2012-06-27 83 views
5

是否可以查詢嵌套文檔中的特定對象?繼承人的一個例子,MongoDB從嵌套文檔檢索選定對象

Collection : Threads 
{ 
    Documents : Messages 
    { 
     threadId = 1 
     messages = [ 
      { 
       user = amy 
       date = 01/01/2012 
       content = hey 
      }, 
      { 
       user = bell 
       date = 01/01/2012 
       content = hey 
      }, 
      { 
       user = bell 
       date = 01/02/2012 
       content = whats up 
      } 
     ] 
    }, 
    { 
     threadId = 2 
     messages = [ 
      { 
       user = courtney 
       date = 01/03/2012 
       content = first! 
      } 
     ] 
    } 
} 

我想我的查詢說{ threadId : 1, 'messages.date' : { $gt : 01/01/2012 } }, { fields : { messages : 1 } }。但它會返回所有的文件信息,當我真的想要結果是這樣的時,

messages = [ 
    { 
     user = bell 
     date = 01/02/2012 
     content = whats up 
    } 
] 
+0

使用MongoDB Aggregations管道可以爲您提供解決方案! –

回答