2017-05-03 2 views
0

次のように私は私が特定の国のために都市を取得したい enter image description here1つのフィールドでクエリを実行し、mongooseで特定のフィールドを取得する方法は?

、コレクションを持っています。どのようなクエリをする必要がありますか?

これは私が試みたものです。

db.getCollection('_event').find([ 
{$match: {}}, 
{$project: { 
    cities: { 
    $filter: { 
    input: 'city', 
    as: 'r', 
    cond: {$eq: ['$$r.country', 'Portugal']} 
    } 
    } 
}} 
]) 
+0

[Mongoose、Findで特定のフィールドを選択]の可能な複製(http://stackoverflow.com/questions/24348437/mongoose-select-a-specific-field-with-find) – ippi

+0

画像はポストで便利ですしかし、**それらのない投稿はまだクリアされていることを確認してください**。データ形式のスクリーンショットを表示する代わりに、実際のデータをコピーして貼り付けるか、直接ポストに入力してください。 cf http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors –

答えて

2

あなたがしようとしている方法は、(変更の使用集約のための)集約

db.getCollection('_event').aggregation([ 
{$match: {}}, 
{$project: { 
    cities: { 
    $filter: { 
    input: 'city', 
    as: 'r', 
    cond: {$eq: ['$$r.country', 'Portugal']} 
    } 
    } 
}} 
]) 

または単純な中で実装されたパイプラインの方法である従って

db.getCollection('_event').find(
    {'location.country':'Portugal'},{'location.city':1} 
    ) 
0

として使用してくださいは、 (モデル全体を取得する) -

1 db.getCollection('_event').find({"location.country":"Portugal"}) 
2 db.getCollection('_event').find({"location.country":\Portugal\}) 

第2位はポルトガル同様です。

ありがとうございます。

関連する問題