本文介绍了如何使用$ unwind保持文档聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

说我有三个学生...

Lets say I have three students...

爱丽丝,她总是在星期五。

Alice, she is Always there on fridays.

{
    "name" : "Alice",
    "goes" : {
        "mondays" : {
            "fr" : 900,
            "to" : 1400
        },
        "fridays" : {
            "fr" : 700,
            "to" : 1600
        },
    }
}

还有鲍勃,这里应该是第一个一月

And bob, here should be there on the first of january

{
    "_id" : ObjectId("5284a7085d60338b40b8f17d"),
    "name" : "Bob",
    "goes" : {
        "mondays" : {
            "fr" : 800,
            "to" : 1200
        },
        "special" : [
            {
                "date" : "2010-01-01",
                "fr" : 1000,
                "to" : 1500
            }
        ]
    }
}

和克莱尔谁不会周一参加r为10.00

And Clair who will not be attenging on mondays or at 10.00

{
    "_id" : ObjectId("5284c2785d60338b40b8f17f"),
    "name" : "Clair",
    "goes" : {
        "wednesdays" : {
            "fr" : 1100,
            "to" : 1500
        },
        "special" : [
            {
                "date" : "2010-01-01",
                "fr" : 1600,
                "to" : 1900
            },
            {
                "date" : "2010-01-02",
                "fr" : 1000,
                "to" : 1300
            }
        ]
    }
}

我想找到所有应该

所以我在汇总框架中做到这一点。

So I do this with the aggregation framework.

db.students.aggregate(
    [
        {
            $unwind: "$goes.special"
        },
        {
            $match: {
                $or : [
                    {
                        'goes.fridays.fr': 700,
                    },
                    {
                        'goes.special.date' : '2010-01-01',
                        'goes.special.fr': 1000
                    }
                ]
            }
        }
    ]
)

但是Alice没有出现。它明确指出了为什么在mongodb文档中在最底部。

But Alice does not show up. It clearly states why in the mongodb docs, http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/ at the very bottom.

我可以通过添加一个带有空值的数组来解决它,但这并不像一个好的解决方案。

I could solve it by adding an array with a null value in it but that does not seam like a nice solution.

是否有一种方法可以让我放心,不要忽略在$ unwind'ed数组中没有数据的文档?

Is there a way I could get unwind NOT to ignore documents that does not have data in a $unwind'ed array?

推荐答案

您完全不需要 $ unwind 。简单的 $ match 在管道中就足够了:

You don't need $unwind at all. Simple $match in pipeline is enough:

pipeline = [
    {
        "$match" : {
            "$or" : [
                {
                    "goes.fridays.fr" : 700
                },
                {
                    "goes.special" : {
                        "$elemMatch" : {
                            "date" : "2010-01-01",
                            "fr" : 1000
                        }
                    }
                }
            ]
        }
    }
]

db.students.aggregate(pipeline)

即使没有聚合框架也可以轻松实现。

It can be done easily even without aggregation framework.

query = {
    "$or" : [
        {
            "goes.fridays.fr" : 700
        },
        {
            "goes.special" : {
                "$elemMatch" : {
                    "date" : "2010-01-01",
                    "fr" : 1000
                }
            }
        }
    ]
}

db.students.find(query)

这篇关于如何使用$ unwind保持文档聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:50