私の考えを言葉で正確に説明することはできないので、ここに例を示します。
[
{
'description': 'fruits',
'examples': [
{
'name': 'Apple',
'color': ['red', 'green']
},
{
'name': 'Banana',
'color': 'yellow'
}
]
},
{
'description': 'vegetables',
'examples': [
{
'name': 'Tomato',
'color': 'red'
},
{
'name': 'Carrot',
'color': 'orange'
}
]
},
{
'description': 'Wweets',
'examples': [
{
'name': 'Chocolate',
'color': ['brown', 'black', 'white']
},
{
'name': 'Candy',
'color': 'various'
}
]
}
]
一歩一歩進みましょう:
すべての食品カテゴリを表示する場合は、次のコマンドでクエリを実行します
db.food.find()
野菜が見たい
db.food.find({ 'description': 'vegetables' })
さて、キャロットの見た目を忘れたとしましょう(笑)。私は何をしますか?私は以下を試しました(ネイティブnode.js MongoDBドライバー):
collection.find({'examples.name': 'Carrot'}, function(err, example){
console.log(example)
// It still returns me the whole object!
});
その結果、MongoDB がオブジェクトの次に高いインスタンスを返すことを期待していました。たとえば、私はこれをやりたかった。
console.log(example.color)
// 'orange'
何か考えはありますか?ドキュメント指向の DB は初めてです :/