对于下面给出的样本集合,我需要地址数组中的所有街道名称。我们应该如何单独找到特定元素的值?我们可以使用聚合
{
"_id" : ObjectId("4c4b1476238d3b4dd5000002"),
"username" : "pbakkum",
"first_name" : "Peter",
"last_name" : "Bakkum",
"age" : 21,
"addresses" : [
{
"name" : "home",
"street" : "588 5th Street",
"city" : "Brooklyn",
"state" : "CA",
"zip" : 11215
},
{
"name" : "work",
"street" : "588 5th Street",
"city" : "Brooklyn",
"state" : "CA",
"zip" : 11215
},
{
}
]
}
{
"_id" : ObjectId("4c4b1476238d3b4dd5000001"),
"username" : "kbanker",
"email" : "kylebanker@gmail.com",
"first_name" : "Kyle",
"last_name" : "Banker",
"hashed_password" : "bd1cfa194c3a603e7186780824b04419",
"addresses" : [
{
"name" : "home",
"street" : "588 5th Street",
"city" : "Brooklyn",
"state" : "NY",
"zip" : 11215
},
{
"name" : "work",
"street" : "1 E. 23rd Street",
"city" : "New York",
"state" : "NY",
"zip" : 10010
}
],
"payment_methods" : [
{
"name" : "VISA",
"last_four" : 2127,
"crypted_number" : "43f6ba1dfda6b8106dc7",
"expiration_date" : ISODate("2016-05-01T07:00:00Z")
}
],
"age" : 151
}
对于下面给出的样本集合,我需要地址数组中的所有街道名称。我们应该如何单独找到特定元素的值?我们可以使用聚合 任何帮助将不胜感激
答案 0 :(得分:0)
您可以尝试将$ map运算符用于聚合enter link description here。
db.collection.aggregate([
{
"$project": { "_id": 0, "data": {
"$map": { "input": "$addresses", "as": "add", "in": "$$add.street" } } }
}
])