我想在当前查询结果上区分name
,我怎么能在MongoDB上做
查询语法
db_conn.medical_records.find({"$or": [{"symptom_1": "nose allergic"},{"symptom_2": "nose allergic"}]})
当前查询结果,但不在名称
上应用distinct{
"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"
},
根据现有查询
应用不同的运算符我想在当前查询结果上区分name
,我怎么能在MongoDB上做
查询语法
db_conn.medical_records.find({"$or": [{"symptom_1": "nose allergic"},{"symptom_2": "nose allergic"}]})
当前查询结果,但不在名称
上应用distinct{
"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"]
答案 0 :(得分:1)
db_conn.medical_records.distinct("name",
{
"$or": [
{ "symptom_1": <your symptom> },
{ "symptom_2": <your symptom> }
]
}
)
答案 1 :(得分:1)
您需要先修复密钥名称。如果是&#39; sympton_1&#39;请修复或者&#39; symptons_1&#39;然后在您的收藏中应用mongo distinct。在distinct中,您也可以指定条件。
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
]
}
)