如何返回字符串数组MongoDB的聚集

如何返回字符串数组MongoDB的聚集

本文介绍了如何返回字符串数组MongoDB的聚集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用MongoDB的聚集返回的字符串数组。我做了以下内容:

  db.users.aggregate([{$组:{_id:$ emails.address}}])

它返回:

  {_id:[email protected]]}
{_id:[[email protected]]}
{_id:[[email protected]]}

有没有办法返回的字符串数组像这样的:

  [[email protected][email protected][email protected]]

非常感谢你的人你的时间对我的帮助谁。

修改

添加数据:

  {
    _id:ukn9MLo3hRYEpCCty,
    createdAt:ISODate(2015-10-24T03:52:11.960Z),
    电子邮件:
        {
            地址:[email protected]
            验证:假的
        }
    ]
}
{
    _id:5SXRXraariyhRQACe,
    createdAt:ISODate(2015-10-24T03:52:12.093Z),
    电子邮件:
        {
            地址:[email protected]
            验证:假的
        }
    ]
}
{
    _id:WMHWxeymY4ATWLXjz,
    createdAt:ISODate(2015-10-24T03:52:12.237Z),
    电子邮件:
        {
            地址:[email protected]
            验证:假的
        }
    ]
}


解决方案

.aggregate()方法总是返回对象无论你做什么,不能改变。

有关你的目的,你最好还是不要使用代替,这并只返回不同值的数组:

db.users.distinct(emails.address);

这正是你想要的输出:

[[email protected][email protected][email protected]]

如果你真的想使用 .aggregate()然后穿越 - 只值需要发生在后处理的前pression的外部。而且你也应该使用 $放松像这样的阵列的时候。

您可以用JavaScript做到这一点 .MAP()例如:

db.users.aggregate([
    {$开卷:$电子邮件},
    {$基:{_id:$ emails.address}}
])。图(功能(EL){返回el._id})

可以得到相同的输出,但是 .MAP()确实改造客户端,而不是在服务器上。

I need to return array of string with mongodb aggregation. I did the following:

db.users.aggregate([{$group: {_id:"$emails.address"}}])

It return:

{ "_id" : [ "[email protected]" ] }
{ "_id" : [ "[email protected]" ] }
{ "_id" : [ "[email protected]" ] }

Is there a way to return array of string like this one:

["[email protected]","[email protected]","[email protected]"]

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" : "[email protected]",
            "verified" : false
        }
    ]
}
{
    "_id" : "5SXRXraariyhRQACe",
    "createdAt" : ISODate("2015-10-24T03:52:12.093Z"),
    "emails" : [
        {
            "address" : "[email protected]",
            "verified" : false
        }
    ]
}
{
    "_id" : "WMHWxeymY4ATWLXjz",
    "createdAt" : ISODate("2015-10-24T03:52:12.237Z"),
    "emails" : [
        {
            "address" : "[email protected]",
            "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:

["[email protected]","[email protected]","[email protected]"]

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的聚集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:59