2017-05-03 56 views
0

我有一個集合如下, enter image description here如何查詢一個字段並獲取貓鼬的特定字段?

我想獲得特定國家的城市。應該查詢什麼?

這是我試過的。

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

可能重複[Mongoose,選擇一個特定的字段與查找](http://stackoverflow.com/questions/24348437/mongoose-select-a-specific-field-with-find) – ippi

+0

圖像是有用的一篇文章,但**確保沒有他們的帖子仍然清晰**。不要顯示數據格式的屏幕截圖,而是直接複製並粘貼或將實際數據輸入到帖子中。 參見http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-orrors –

回答

2

請使用如下

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

你已經嘗試過的方式是管道方式是實現與聚集(對於改性用聚合)

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

或簡單方式(獲取整個模型): -

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

2nd與葡萄牙相同

謝謝。