2016-05-25 57 views
0

例如,我有這樣的結構:MongoDB,如何在不指定密鑰的情況下查詢MongoDB中的子對象?

{ 
    name:{ 
     "en":"london", 
     "fr":"londres", 
     "sq":"londra" 
    }, 
    ... 
}, 
{ 
    name:{ 
     "de":"barcelona", 
     "sv":"barcelone" 
    }, 
    ... 
} 
... 

我想知道我可以在本例中檢索,所有的城市,其名稱中包含「結腸」,但沒有指定鍵(「德」或「 FR「)?

所以,不是這個:

db.cities.find({$or:{"name.en":/lon/,"name.fr":/lon/, ...}}) 

但這樣的:

db.cities.find({"name":/lon/}}) 
-> find in the children of "name, don't care about the key 

回答

0

得到這個你可以創建一個text index,其中包括所有的字段:

db.collection.createIndex ({「$ **」:「text」, })

,然後使用您的查詢$search - more here

db.cities.find({ $text: { 
      $search: "lon", 
      $caseSensitive: true, 
     $diacriticSensitive: true 
    } }) 
+0

謝謝,但文本索引僅適用於完整的莖的話... –

相關問題