你好,我有以下数据结构:
[
{
"name": "a name",
"project": [
{
companyName: "a name",
contactPerson: [
{
work_email: "test@test.com"
}
]
},
{
companyName: "a name1",
contactPerson: [
{
work_email: "test1@test.com"
}
]
},
{
companyName: "a name2",
contactPerson: [
{
work_email: "test2@test.com"
}
]
},
{
companyName: "a name3",
contactPerson: [
{
work_email: "test@test.com"
}
]
},
]
}
]
通过此查询,我想找到所有带有电子邮件test@test.com的项目:
db.collection.find({
"project.contactPerson.work_email": "test@test.com"
},
{
"project.$": 1
})
它仅返回找到的第一个结果,然后停止。但是在我的数据中,我有两个带有该电子邮件的项目,我想找到两个项目。如果可以的话,这是一个游乐场,您可以用来进一步帮助我。在此先感谢您,并非常感谢:https://mongoplayground.net/p/4Mpp7kHi98u
答案 0 :(得分:1)
位置$运算符将an的内容限制为返回以下任意一个:
您可以执行以下操作
# filter out Group B and add a column with desire colour vector
m <- df %>%
select(Group, Proband.ID) %>%
filter(Group Group == "B") %>%
unique() %>%
mutate(cols = col_vector[1:17])
#filter out group A
s <- t %>%
select(Group, Proband.ID) %>%
filter(Group == "A") %>%
unique()
# make a list containing the colour vector from `m` data frame and add empty strings to the size of "A" Proband's
mm <- list(prob = c(m$cols, (rep("",length(s$Proband.ID)))))
# name the list by concatenating in order and use this list in pheatmap annotatioin
names(mm$prob) <- c(as.character(m$Proband.ID), as.character(s$Proband.ID))
答案 1 :(得分:1)
db.collection.aggregate([
{
$unwind: "$project"
},
{
$match: {
"project.contactPerson.work_email": "test@test.com"
}
},
{
"$group": {
"_id": "$_id",
"name": {
"$first": "$name"
},
"project": {
"$push": {
"companyName": "$project.companyName",
"contactPersion": "$project.contactPerson"
}
}
}
}
])
答案 2 :(得分:0)
// step1(与问题语句相关的查找):,即使联系人的数组元素之一匹配,查找也显示所有项目,使用聚合函数显示特定的电子邮件ID
> db.test3.find({ "project.contact.email": "abc2@email.com" }).pretty();
{
"_id" : ObjectId("5f43fdc153e34ac6967fe8ce"),
"name" : "Pega Contractors",
"project" : [
{
"pname" : "pname1",
"contact" : [
{
"email" : "xyz1@email.com"
}
]
},
{
"pname" : "pname2",
"contact" : [
{
"email" : "abc2@email.com"
}
]
},
{
"pname" : "pname3",
"contact" : [
{
"email" : "xyz1@email.com"
}
]
}
]
}
--
//aggregate option:
//Step1: data preparation
> db.test3.find().pretty();
{
"_id" : ObjectId("5f43fdc153e34ac6967fe8ce"),
"name" : "Pega Contractors",
"project" : [
{
"pname" : "pname1",
"contact" : [
{
"email" : "xyz1@email.com"
}
]
},
{
"pname" : "pname2",
"contact" : [
{
"email" : "abc2@email.com"
}
]
},
{
"pname" : "pname3",
"contact" : [
{
"email" : "xyz1@email.com"
}
]
}
]
}
>
//step2: aggregate and unwind project for the next step pipeline input
> db.test3.aggregate([ {$unwind: "$project"}]);
{ "_id" : ObjectId("5f43fdc153e34ac6967fe8ce"), "name" : "Pega Contractors", "project" : { "pname" : "pname1", "contact" : [ { "email" : "xyz1@email.com" } ] } }
{ "_id" : ObjectId("5f43fdc153e34ac6967fe8ce"), "name" : "Pega Contractors", "project" : { "pname" : "pname2", "contact" : [ { "email" : "abc2@email.com" } ] } }
{ "_id" : ObjectId("5f43fdc153e34ac6967fe8ce"), "name" : "Pega Contractors", "project" : { "pname" : "pname3", "contact" : [ { "email" : "xyz1@email.com" } ] } }
//step3: Desired outcome, i.e display data specific to email
> db.test3.aggregate([
... {$unwind: "$project"},
... {$match: {"project.contact.email":"xyz1@email.com"}}
... ]);
{ "_id" : ObjectId("5f43fdc153e34ac6967fe8ce"), "name" : "Pega Contractors", "project" : { "pname" : "pname1", "contact" : [ { "email" : "xyz1@email.com" } ] } }
{ "_id" : ObjectId("5f43fdc153e34ac6967fe8ce"), "name" : "Pega Contractors", "project" : { "pname" : "pname3", "contact" : [ { "email" : "xyz1@email.com" } ] } }
> db.test3.aggregate([ {$unwind: "$project"}, {$match: {"project.contact.email":"acb2@email.com"}} ]);
> db.test3.aggregate([ {$unwind: "$project"}, {$match: {"project.contact.email":"abc2@email.com"}} ]);
{ "_id" : ObjectId("5f43fdc153e34ac6967fe8ce"), "name" : "Pega Contractors", "project" : { "pname" : "pname2", "contact" : [ { "email" : "abc2@email.com" } ] } }
>