我有这个代码
> db.attributes.aggregate([{ $match: {"name.en-UK": "Size"} },{ $unwind : "$values" },{ $project: { "code" : "$values.code", "name" : "$values.name", "_id": "$values._id"} },{ $match: {"name.en-UK": "S"}}])
{
"result" : [
{
"_id" : ObjectId("53b2c644d901f244965f74e3"),
"code" : 0,
"name" : {
"en-UK" : "S"
}
},
{
"_id" : ObjectId("53b2c644d901f244965f74e4"),
"code" : 1,
"name" : {
"en-UK" : "S"
}
}
],
"ok" : 1
}
如何更改它以便它只返回"代码" == 0 with" name" ==" en-UK" :" S"
我试过这个:
> db.attributes.aggregate([{ '$match': {"name.en-UK": "Size"} },
... { '$unwind' : "$values" },
... { '$project': { "code" : "$values.code", "valueId": "$values._id"} },
... { '$match': {
... $and: [
... {"name.en-UK": "S"},
... {"code": 0}
... ]
... }
... }])
{ "result" : [ ], "ok" : 1 }
但是没有用!
和建议非常感谢
答案 0 :(得分:0)
似乎我需要向select
添加内容,因此{ $project: { "code" : "$values.code", "name" : "$values.name", "_id": "$values._id"} }
必须包含名称
> db.attributes.aggregate([{ '$match': {"name.en-UK": "Size"} }, { '$unwind' : "$values" }, { $project: { "code" : "$values.code", "name" : "$values.name", "_id": "$values._id"} }, { '$match': { $and: [ {"name.en-UK": "S"}, {"code": 0} ] } }])
{
"result" : [
{
"_id" : ObjectId("53b2c644d901f244965f74e3"),
"code" : 0,
"name" : {
"en-UK" : "S"
}
}
],
"ok" : 1
}