如何使用多个条件进行查询并且这些条件是相关的 [英] How to query with multiple conditions and those conditions are dependent
问题描述
假设症状_N中的A001"、A002"、A003"表示鼻子过敏"
假设症状_N中的Z001"、Z002"表示鼻癌"
我想找到那些患鼻癌并且在患癌前鼻过敏的人.
I want to find those who got nose cancer AND the had got nose allergic before getting cancer.
比如下面2条记录命中了我想要的目标.
For example, the following 2 records hit the target I want.
我可以推断 Jack 在 2015-04-02 得了鼻癌",
I can inferred Jack got "nose cancer" on 2015-04-02,
他在 2011-04-02 出现了鼻子过敏".
and he had got "nose allergic" on 2011-04-02.
我可以使用 $or 聚合 运算符找到鼻子过敏 记录.像 db.collection.find({"$or": OR_CONDITIONS})
I can find the nose allergic records with $or aggregation operator. like db.collection.find({"$or": OR_CONDITIONS})
我不知道如何在 MongoDB 中完成复合条件查询.
I have no idea how to finish the compounded conditions query in MongoDB.
{
"name": "Jack",
"symptoms_1": "B00 ",
"symptoms_2": "A001 ",
"symptoms_3": " ",
"datetime": "2011-04-02"
},
....
{
"name": "Jack",
"symptoms_1": "",
"symptoms_2": "",
"symptoms_3": "Z001",
"datetime": "2015-04-02"
},
推荐答案
您将条件放在 [ {}, {}, {}, {}] 数组中(因为数组是有效的 json).
you put the conditions inside an [ {}, {}, {}, {}] array (since an array is valid json).
db.inventory.find( { $or: [ { "symptom_1": "Z001" }, {"symptom_2": "Z002" }] })
事实上,您可能正在寻找适用于公共字段的 $in 运算符
in fact, you might be seeking the $in operator that works on a common field
db.collection.find({ "symptom_1": { $in: ["Z001", "Z002", "A001", "A002", "A003"]});
而且您似乎想要梳理所有症状字段,因此同时使用 $or 和 $in
and it seems you want comb thru all symptom fields so use both $or and $in as such
db.collection.find({$or:
[
{"symptom_1": { $in: ["Z001", "Z002", "A001", "A002", "A003"]}},
{"symptom_2": { $in: ["Z001", "Z002", "A001", "A002", "A003"]}} ,
...
]} );
大括号可能不匹配,但从那开始.
the braces might be mismatched but start off with that.
这篇关于如何使用多个条件进行查询并且这些条件是相关的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!