我有一个类似的集合:
{
"_id" : ObjectId("57dc2b63add5c5e91f185fe0"),
"level" : 2,
"profili" : [
{
"_id" : ObjectId("507f1f77bcf86cd799439011"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
"costopag" : 1,
"pagmax" : 10,
"pagmin" : 1
},
{
"_id" : ObjectId("507f191e810c19729de860ea"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
"costopag" : 0.5,
"pagmax" : 100,
"pagmin" : 11
}
],
"__v" : 0
},
{
"_id" : ObjectId("57dc39945aec26b303053dd7"),
"level" : 2,
"profili" : [
{
"_id" : ObjectId("507f1f77bcf86cd799439012"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
"costopag" : 10,
"pagmax" : 100,
"pagmin" : 11
}
]
},
{
"_id" : ObjectId("57e7ff05a0ef6d07c3048d7f"),
"level" : 2,
"profili" : [
{
"_id" : ObjectId("507f1f77bcf86cd799439013"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498c"),
"costopag" : 0.5,
"pagmax" : 10,
"pagmin" : 1
}
]
},
{
"_id" : ObjectId("57e7ff54a0ef6d07c3048d81"),
"level" : 2,
"profili" : [
{
"_id" : ObjectId("507f1f77bcf86cd799439014"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498c"),
"costopag" : 5,
"pagmax" : 10,
"pagmin" : 1
}
],
"__v" : 0
}
我需要搜索一个具有特定“profili”的文档。 如果我执行此查找:
database.Utente.find({
"level":2,
profili: {
$elemMatch:{
idprofilo: '57fe3c51569b5c0afb88498b',
pagmin: {$lte:2},
pagmax: {$gte:2}
}
}
})
结果是:
{
"_id" : ObjectId("57dc2b63add5c5e91f185fe0"),
"level" : 2,
"profili" : [
{
"_id" : ObjectId("507f1f77bcf86cd799439011"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
"costopag" : 1,
"pagmax" : 10,
"pagmin" : 1
},
{
"_id" : ObjectId("507f191e810c19729de860ea"),
"idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
"costopag" : 0.5,
"pagmax" : 100,
"pagmin" : 11
}
],
"__v" : 0
}
如果可能,我不会profili.id = 507f191e810c19729de860ea,因为没有应要求回应。 我已经阅读过.aggregate和$ project运算符可能,但我不明白我是怎么做的。
答案 0 :(得分:0)
您需要在aggregate
阶段$project
使用db.test.aggregate([
{
$match: {
level: 2
}
},
{
$project: {
level: 1,
profili: {
$filter: {
input: '$profili',
as: 'prof',
cond: {
$and : [
{$eq: ['$$prof.idprofilo', ObjectId('57fe3c51569b5c0afb88498b')]},
{$lte: ['$$prof.pagmin', 2]},
{$gte: ['$$prof.pagmax', 2]}
]
}
}
}
}
}
])
:
profili
如果您要删除空$match
的所有文档,只需添加另一个{
$match: {
profili: { $nin: [[], null] }
}
}
阶段:
nslookup 204.228.150.3