本文介绍了我怎样才能聚集在文件里面阵列MongoDB中,并得到计数多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序,它在车辆复燃数据的XML文件,并将其转换成JSON,因此它可以被存储在MongoDB数据库。在XML开头是这样的:

I am writing a program that takes in an XML file of vehicle reflash data and converts it to JSON so it can be stored in a MongoDB database. The XML starts like this:

<FlashReportGeneratorTag>
    <VehicleEntry>
        <VehicleStatus>PASSED</VehicleStatus>
    </VehicleEntry>
    <VehicleEntry>
        <VehicleStatus>PASSED</VehicleStatus>
    </VehicleEntry>
</FlashReportGeneratorTag>

在我把它转换成JSON和添加项目标识符我留下还挺像这样的格式:

After I convert it to JSON and add the project identifier I am left with a format kinda like this:

{
    "FlashReportGeneratorAddedTag" : {
        "VehicleEntry" : [
            {
                "VehicleStatus" : "PASSED"
            },
            {
                "VehicleStatus" : "PASSED"
            }
        ]
    },
    "project_id" : "1234"
}

我想这样做的就是通过车辆和车辆数数的合计数在每个文档中失败的项目1234,但我有没有运气。

What I would like to do is get an aggregate count of number of vehicles passed and number of vehicles failed within each document for project 1234 but I have had no luck.

我已经使用基本聚集技能我试着知道,但我不能简单地通过PROJECT_ID集团自由的文件,将其组合,当我需要一个数组聚集在它里面。我还没有发现,告诉你任何资源,如果你能或者不能一次聚集两个值(获得的总和通过和失败次数的总和)。

I have tried using the basic aggregation skills I know but I cannot simply group by project_id since that will group by document, when I need to aggregate over an array inside of it. I also haven't found any resources that tell you if you can or cannot aggregate two values at once (get sum of passed and sum of failed counts).

作为一个非常万不得已我可以改变文档样式各地的只是每个VehicleEntry将自己的文件,但我想借此和存储XML,因为它是如果我能

As a very last resort I could change the document style around to just have each VehicleEntry be its own document, but I would like to take and store the XML as it is if I can.

修改是开卷我能够设置为我要找的阵列聚合:

EDIT Using Unwind I was able to setup an aggregation for the array that I'm looking for:

var aggregate = collection.Aggregate().Match(new BsonDocument { { "project_id", "1234" } }).Unwind(i => i["FlashReportGeneratorAddedTag.VehicleEntry"]);



不过,我找不到将这些正确的方法,以在整个拿到合格/不合格计数的阵列。我认为有一些办法,我需要使用匹配功能,但我无法弄清楚如何做到这一点,但不排除的两个条件之一。我必须运行两次聚集,一度为通过,一旦失败?

However, I cannot find the proper way to group these in order to get the pass/fail counts throughout the array. I assume there is some way I need to use the Match function but I can't figure out how to do that without excluding one of the two conditions. Do I have to run aggregation twice, once for passed and once for failed?

推荐答案

感谢来自JohnnyHK并暗示一些挖我可以工作了这一点。首先,我不得不使用开卷的方法来放松身心的 Vehicleentry 阵列,以汇总就可以了:

Thanks to a hint from JohnnyHK and some more digging I was able to work this out. First I had to use the Unwind method to unwind the Vehicleentry array in order to aggregate on it:

var aggregate = collection.Aggregate().Match(new BsonDocument { { "project_id", "1234" } })
   .Unwind(i => i["FlashReportGeneratorAddedTag.VehicleEntry"])

有一次,我说我是能够巢 BsonDocuments 为了总结基于一个条件。为了获得通过计数我用这样的:

Once I had that I was able to nest BsonDocuments in order to sum based on a condition. In order to get the passed count I used this:

{ "passed", new BsonDocument { { "$sum", new BsonDocument { { "$cond", new BsonArray { new BsonDocument { { "$eq", new BsonArray { "$FlashReportGeneratorAddedTag.VehicleEntry.VehicleStatus", "PASSED" } } }, 1, 0 } } } } } }

同样地,我在一个失败的选项卡中添加。整个事情(尚未格式化)是这样的:

Similarly I added in a failed tab. The whole thing (not yet formatted) was like this:

var collection = _database.GetCollection<BsonDocument>(Vehicles);
var aggregate = collection.Aggregate()
.Match(new BsonDocument{ { "project_id", "1234" } })
.Unwind(i => i["FlashReportGeneratorAddedTag.VehicleEntry"])
.Group(new BsonDocument
{
    { "_id", "$project_id" },
    { "passed", new BsonDocument
        { { "$sum", new BsonDocument
                { { "$cond", new BsonArray
                        { new BsonDocument
                            { { "$eq", new BsonArray
                                {
                                    "$FlashReportGeneratorAddedTag.VehicleEntry.VehicleStatus",
                                    "PASSED"
                                } }
                                },
                            1,
                            0 } } } } }
    },
    { "failed", new BsonDocument
        { { "$sum", new BsonDocument
                { { "$cond", new BsonArray
                        { new BsonDocument
                            { { "$eq", new BsonArray
                                {
                                    "$FlashReportGeneratorAddedTag.VehicleEntry.VehicleStatus",
                                    "FAILED"
                                } }
                                },
                            1,
                            0 } } } } }
    },
});
var results = await aggregate.ToListAsync();

这篇关于我怎样才能聚集在文件里面阵列MongoDB中,并得到计数多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 02:31