Same field error in aggregation _id

社会主义新天地 提交于 2019-12-13 18:18:55

问题


I have a collection in mongodb like this format :

"_id" : ObjectId("542d5256372943d9e000fc42"),
    "_class" : "com.bedas.ays.mongo.dto.AboneAriza",
    "AdSoyad" : "AKIN COBAN",
    "aimid" : 100009,
    "CepTelefon" : "5332234401",
    "Telefon" : "0",
    "fiderid" : 101383,
    "kofrano" : NumberLong(444454),
    "tcNo" : "26402622688",
    "tmno" : {
        "trafoNo" : 21649,
        "trafoname" : "21450 BÜYÜKŞEHİR FİDERİ-->21646 BEYKOP 2.BÖLGE"
    },
    "ilce" : {
        "ilceUavt" : 2051,
        "ilcename" : "BEYLİKDÜZÜ"
    },
    "mahalle" : {
        "mahalleUavt" : 40875,
        "mahname" : "BARIŞ"
    },
    "sokak" : {
        "_id" : 716802,
        "name" : "EGEMENLİK CAD."
    },
    "bina" : {
        "_id" : 15600414,
        "name" : "ERCİYES APT. A-1"
    },
    "longitude" : "41.00345215",
    "latitude" : "28.65782942",
    "aboneno" : "00005181209",
    "bolge" : 1,
    "sube" : {
        "_id" : 4,
        "name" : "AVCILAR",
        "Bolgeid" : 0
    },
    "kademeler" : [ 
        {
            "kademeId" : 1567,
            "isemriId" : 3661,
            "elektrikselAdresNo" : 21649,
            "isemirleri" : {
                "_id" : null,
                "id" : 3661,
                "isemriNo" : 3661,
                "anahtarKelime" : "",
                "aciklama" : "deneme erkan1",
                "emreAmade" : false,
                "degistirmeSebebi" : "",
                "acil" : false,
                "longitude" : "28.65831040",
                "latitude" : "41.00287477",
                "il" : {
                    "_id" : 1,
                    "name" : "ISTANBUL"
                },
                "basvuruTuru" : {
                    "_id" : 1,
                    "name" : "Bildirimli",
                    "id" : 1
                },
                "kesintiTipi" : {
                    "_id" : 1,
                    "name" : "AG şube",
                    "id" : 1
                },
                "havaKosulu" : {
                    "_id" : 1,
                    "name" : "Açık",
                    "id" : 1
                },
                "kesintiDurumu" : {
                    "_id" : 1,
                    "name" : "Tamir edildi",
                    "id" : 2
                },
                "gerilimTuru" : {
                    "_id" : 1,
                    "name" : "34,5 KV",
                    "id" : 1
                },
                "sebekeUnsuru" : {
                    "_id" : 1,
                    "name" : "TM",
                    "id" : 1
                },
                "isemriKaynagi" : {
                    "_id" : 1,
                    "name" : "İletim",
                    "id" : 1
                },
                "birimi" : {
                    "_id" : 1,
                    "name" : "Kesinti",
                    "subeid" : 0,
                    "id" : 1
                },
                "isemriSebebi" : {
                    "_id" : 1,
                    "name" : "Mücbir sebep",
                    "id" : 1
                },
                "isemriSureTipi" : {
                    "_id" : 1,
                    "name" : "Uzun",
                    "id" : 1
                },
                "arizaKapatmaTarihi" : null,
                "baslamaZamani" : null,
                "tahminiBitisZamani" : null,
                "adres" : "    ",
                "basvuruSayisi" : 2,
                "sonaErmeZamani" : null,
                "kesintiSuresiSaat" : null,
                "etkilenenKullaniciSayisi" : null,
                "imarAlaniAg" : null,
                "imarAlaniOg" : null,
                "toplamEtkilenmeSureAg" : null,
                "toplamEtkilenmeSureOg" : null,
                "emreAmadeTarihi" : null,
                "emreAmadeSureOg" : null,
                "isemriBaslatan" : null,
                "isemriDegistiren" : null,
                "isemriKapatan" : null,
                "kofraNo" : null,
                "kaydeden" : null,
                "ihbarTarihi" : null,
                "direkNo" : null
            }
        }
    ]
}

I use spring-data for querying in this collection and my query is like this one :

Criteria criteria = Criteria
                    .where("kademeler.isemirleri.basvuruTuru.id").is(2)
                    .and("kademeler.isemirleri.isemriSureTipi.id").is(1)
                    .and("kademeler.isemirleri.kesintiDurumu.id").is(2);

            Aggregation aggregation = newAggregation(
                    match(criteria),
                    unwind("kademeler"),
                    match(criteria),
                    group("kademeler.isemirleri.isemriKaynagi.name",
                            "kademeler.isemirleri.isemriSebebi.name").count()
                            .as("etkilenenAboneSayisi")
                    );

But the name field of both isemriSebebi and isemriKaynagi have the same field named name.When I mapped to an object the aggregationResult it gives an error 'Same field error' What can i do to avoid this error ? Because all of my class that embedded document have the same field named name.


回答1:


Not the clearest documented point. You need a different representation of the Fields object here to define your multiple field condition properly:

Aggregation aggregation = newAggregation(
    match(criteria),
    unwind("kademeler"),
    match(criteria),
    group(
        Fields.from(
            Fields.field("name1", "kademeler.isemirleri.isemriKaynagi.name"),
            Fields.field("name2", "kademeler.isemirleri.isemriSebebi.name")
        )).count().as("etkilenenAboneSayisi")
);

That is using the Fields.field definition which has a "name" and a "target" so that it is interpreted correctly. Using plain strings will just result in the default action of stripping everything off until the last "dot" in the field name, and as per your error these are both the same thing. Specifying in this form tells the builder how you want it to be properly handled.

Which basically serializes the $group as:

{ "$group": {
    "_id": {
        "name1": "$kademeler.isemirleri.isemriKaynagi.name",
        "name2": "$kademeler.isemirleri.isemriSebebi.name"
    },
    "etkilenenAboneSayisi": { "$sum": 1 }
}}

Which is what you want.



来源:https://stackoverflow.com/questions/26658044/same-field-error-in-aggregation-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!