如何到达mongodb中的多维数组数据?
how to reach multidimensional array data in mongodb?
我想从以下输出中获取所有 'username':
> doc=db.instagram1.findOne();
{
"_id" : ObjectId("55bfba2bc24e980ec46f59c2"),
"meta" : {
"code" : NumberLong(200)
},
"data" : [
{
"username" : "lifteds0ul",
"profile_picture" : "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/s150x150/11249117_1590613231201553_906635879_a.jpg",
"id" : "2139603325",
"full_name" : "River Sage"
},
{
"username" : "noony__alhosawy",
"profile_picture" : "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/s150x150/11326999_1464806933841520_1635942624_a.jpg",
"id" : "2092127241",
"full_name" : "NEAMA"
},
{
"username" : "_perverse",
"profile_picture" : "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/s150x150/11312547_878996775481318_911042248_a.jpg",
"id" : "2090297101",
"full_name" : ""
},
etc....
在数据值范围内
我的尝试:
> for (data in doc) print(username);
data
data
data
> for (username in doc) print(username);
_id
meta
data
> for (data[username] in doc) print(username);
data
data
data
> for (data:[username] in doc) print(username);
2015-09-06T06:57:34.148+0000 SyntaxError: Unexpected token :
预期输出:
lifteds0ul
noony__alhosawy
_perverse
...
作为第二个警告,如果我能得到 'username' 的计数作为第二个单独的查询,那将非常有帮助。
您只需要 "aggregation" and two projections. In the first $project
stage you use the $map
operator which returns the array of "username" and; in the second you need to use the $size
运算符即可获得数组中 "username" 的计数。
db.instagram1.aggregate([{
"$project": {
"usernames": {
"$map": {
"input": "$data",
"as": "d",
"in": "$$d.username"
}
}
}},
{
"$project": {
"usernames": 1,
"_id": 0,
"count": { "$size": "$usernames" }
}}
])
哪个returns:
{
"usernames" : [
"lifteds0ul",
"noony__alhosawy",
"_perverse"
],
"count" : 3
}
我想从以下输出中获取所有 'username':
> doc=db.instagram1.findOne();
{
"_id" : ObjectId("55bfba2bc24e980ec46f59c2"),
"meta" : {
"code" : NumberLong(200)
},
"data" : [
{
"username" : "lifteds0ul",
"profile_picture" : "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/s150x150/11249117_1590613231201553_906635879_a.jpg",
"id" : "2139603325",
"full_name" : "River Sage"
},
{
"username" : "noony__alhosawy",
"profile_picture" : "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/s150x150/11326999_1464806933841520_1635942624_a.jpg",
"id" : "2092127241",
"full_name" : "NEAMA"
},
{
"username" : "_perverse",
"profile_picture" : "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/s150x150/11312547_878996775481318_911042248_a.jpg",
"id" : "2090297101",
"full_name" : ""
},
etc....
在数据值范围内
我的尝试:
> for (data in doc) print(username);
data
data
data
> for (username in doc) print(username);
_id
meta
data
> for (data[username] in doc) print(username);
data
data
data
> for (data:[username] in doc) print(username);
2015-09-06T06:57:34.148+0000 SyntaxError: Unexpected token :
预期输出:
lifteds0ul
noony__alhosawy
_perverse
...
作为第二个警告,如果我能得到 'username' 的计数作为第二个单独的查询,那将非常有帮助。
您只需要 "aggregation" and two projections. In the first $project
stage you use the $map
operator which returns the array of "username" and; in the second you need to use the $size
运算符即可获得数组中 "username" 的计数。
db.instagram1.aggregate([{
"$project": {
"usernames": {
"$map": {
"input": "$data",
"as": "d",
"in": "$$d.username"
}
}
}},
{
"$project": {
"usernames": 1,
"_id": 0,
"count": { "$size": "$usernames" }
}}
])
哪个returns:
{
"usernames" : [
"lifteds0ul",
"noony__alhosawy",
"_perverse"
],
"count" : 3
}