2012-06-06 55 views
2

我有以下類型的集合MongoDB中:笨用MongoDB的子陣列

Array 
(
[_id] => 4fcf383a5990581c0b000015 
[user_id] => 1 
[username] => admin 
[password] => 21232f297a57a5a743894a0e4a801fc3 
[first_name] => admin 
[last_name] => admin 
[address] => 
[address_2] => 
[deshboard_report] => Array 
    (
     [0] => Array 
      (
       [report_ids] => 1 
       [rpt_color] => color-red 
       [rpt_status] => max 
       [report_title] => Last Point 
       [report_file] => last_location 
      ) 
     [1] => Array 
      (
       [report_ids] => 2 
       [rpt_color] => color-green 
       [rpt_status] => max 
       [report_title] => Inactive Device 
       [report_file] => inactive_devices 
      ) 
    ) 
) 

我想在report_ids搜索和deshboard_report該搜索返回子陣列它是如何可能?

我使用這個庫連接:https://github.com/alexbilbie/codeigniter-mongodb-library

+0

僅供參考同樣的問題已經被問這裏還有:https://groups.google.com/forum/? fromgroups#!話題/ MongoDB的用戶/ NRsk8frOm0U – Barrie

回答

1

當陣列中的查詢元素,您的結果將當前默認返回整個文檔。

您可以使用$ elemMatch返回與具有特定report_id的子數組匹配的整個文檔。

例爲JS殼..找到report_ids = 2文件:

db.collection.find({'deshboard_report': {"$elemMatch": {report_ids: 2}}});