2012-08-29 46 views
-1

我目前正在評估不同數據庫對於用例的效率。在Mongodb中,想要存儲大約100萬個具有以下結構的對象。每個對象在foo數組中都有5到10個對象。索引嵌入式數組的效率

{ 
    name:"my name", 
    foos:[ 
     { 
     foo:"...", 
     bar:"..." 
     }, 
     { 
     foo:"...", 
     bar:"..." 
     }, 
     { 
     foo:"...", 
     bar:"..." 
     } 
    ] 
} 

我經常需要搜索這其中FOOS集合包含的對象具有特定屬性的對象,如:

// mongo collection 
[ 
    { 
     name:'my name', 
     foos:[ 
     { 
      foo:'one_foo', 
      bar:'a_bar' 
     }, 
     { 
      foo:'two_foo', 
      bar:'b_bar' 
     } 
     ] 
    }, 
    { 
     name:'another name', 
     foos:[ 
     { 
      foo:'another foo', 
      bar:'a_bar' 
     }, 
     { 
      foo:'just another foo', 
      bar:'c_bar' 
     } 
     ] 
    } 
] 

// search (pseudo code) 
{ foos: {$elemMatch: {bar: 'c_bar'}} } 
// returns 
{ 
     name:'another name', 
     foos:[ 
     { 
      foo:'another foo', 
      bar:'a_bar' 
     }, 
     { 
      foo:'just another foo', 
      bar:'c_bar' 
     } 
     ] 
    } 

可以這樣有效地與蒙戈來完成,應該如何索引設置? 我不希望你爲我評估性能,只是想法mongo如何執行我的用例或優化的樣子。

回答

2

MongoDB中有文件,說明如何在嵌入式文件創建索引,通過點符號:

Dot Notation (Reaching into Objects)

> db.blogposts.findOne() 
{ title : "My First Post", author: "Jane", 
    comments : [{ by: "Abe", text: "First" }, 
       { by : "Ada", text : "Good post" } ] 
} 
> db.blogposts.find({ "comments.by" : "Ada" }) 

> db.blogposts.ensureIndex({ "comments.by" : 1 }); 

至於性能特點...只是與你的數據集進行測試。

+0

我應該補充說mongo有兩種索引數組的方法。第一個是將它索引爲一個完整的對象,第二個索引每個部分,這個特定的方法是索引每個部分。 – Sammaye