2015-04-04 32 views
0

不同的運營商我想不同的name當前的查詢結果,我怎麼能在MongoDB的應用基於現有的查詢

查詢語法

db_conn.medical_records.find({"$or": [{"symptom_1": "nose allergic"},{"symptom_2": "nose allergic"}]}) 

當前的查詢結果做的,而不是在適用不同命名

{ 
    "id": 1, 
    "name": "Mary",  
    "symptom_1": null, 
    "symptom_2": "nose allergic", 
    "datetime": "2011-04-02" 
}, 

{ 
    "id": 2, 
    "name": "Jack", 
    "symptom_1": "nose allergic", 
    "symptoms_2": null, 
    "datetime": "2011-04-02" 
}, 

{ 
    "id": 3, 
    "name": "Mark", 
    "symptoms_1": null, 
    "symptoms_2": "nose allergic", 
    "datetime": "2010-01-02" 
}, 

{ 
    "id": 4, 
    "name": "Mark", 
    "symptoms_1": "nose allergic", 
    "symptoms_2": "headach", 
    "datetime": "2015-04-04" 
}, 

應用不同的運營商根據exsisting查詢

我想在當前的查詢結果不同的name,我怎麼能不上的MongoDB

查詢語法

db_conn.medical_records.find({"$or": [{"symptom_1": "nose allergic"},{"symptom_2": "nose allergic"}]}) 

當前的查詢結果,但不能在名義申請不同

{ 
    "id": 1, 
    "name": "Mary",  
    "symptom_1": null, 
    "symptom_2": "nose allergic", 
    "datetime": "2011-04-02" 
}, 

{ 
    "id": 2, 
    "name": "Jack", 
    "symptom_1": "nose allergic", 
    "symptoms_2": null, 
    "datetime": "2011-04-02" 
}, 

{ 
    "id": 3, 
    "name": "Jack", 
    "symptoms_1": null, 
    "symptoms_2": "nose allergic", 
    "datetime": "2010-01-02" 
}, 

{ 
    "id": 4, 
    "name": "Mark", 
    "symptoms_1": "nose allergic", 
    "symptoms_2": "headach", 
    "datetime": "2015-04-04" 
} 

預期結果

["Mary", "Jack", "Mark"] 

回答

1

使用collection.distinct

db_conn.medical_records.distinct("name", 
    { 
     "$or": [ 
        { "symptom_1": <your symptom> }, 
        { "symptom_2": <your symptom> } 
       ] 
    } 
) 
1

您需要首先解決的鍵名。請修復它是否爲'sympton_1'或'symptons_1',然後在您的收藏中應用mongo截然不同。不同的是,你也可以指定標準。

db.collection.distinct({ 
    "$or": [ 
       { "symptom_1": "nose allergic" }, 
       { "symptom_2": "nose allergic" } // here you need to fix key name as it is 'symptoms_1' in some documents 
      ] 
}