如何使用 mongodb 聚合返回字符串数组 [英] How to return array of string with mongodb aggregation
问题描述
I need to return array of string with mongodb aggregation. I did the following:
db.users.aggregate([{$group: {_id:"$emails.address"}}])
It return:
{ "_id" : [ "a@a.com" ] }
{ "_id" : [ "b@a.com" ] }
{ "_id" : [ "c@a.com" ] }
Is there a way to return array of string like this one:
["a@a.com","b@a.com","c@a.com"]
thank You very much anyone who taking your time for helping me..
EDIT
Adding data:
{
"_id" : "ukn9MLo3hRYEpCCty",
"createdAt" : ISODate("2015-10-24T03:52:11.960Z"),
"emails" : [
{
"address" : "a@a.com",
"verified" : false
}
]
}
{
"_id" : "5SXRXraariyhRQACe",
"createdAt" : ISODate("2015-10-24T03:52:12.093Z"),
"emails" : [
{
"address" : "b@a.com",
"verified" : false
}
]
}
{
"_id" : "WMHWxeymY4ATWLXjz",
"createdAt" : ISODate("2015-10-24T03:52:12.237Z"),
"emails" : [
{
"address" : "c@a.com",
"verified" : false
}
]
}
The .aggregate()
method always returns Objects
no matter what you do and that cannot change.
For your purpose you are probably better off using .distinct()
instead, which does just return an array of the distinct values:
db.users.distinct("emails.address");
Which is exactly your desired output:
["a@a.com","b@a.com","c@a.com"]
If you really want to use .aggregate()
then the tranformation to just the values needs to happen "outside" of the expression in post processing. And you should also be using $unwind
when dealing with arrays like this.
You can do this with JavaScript .map()
for example:
db.users.aggregate([
{ "$unwind": "$emails" },
{ "$group": { "_id": "$emails.address" } }
]).map(function(el) { return el._id })
Which gives the same output, but the .map()
does the transformation client side instead of on the server.
这篇关于如何使用 mongodb 聚合返回字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!