聚合_id中存在相同的字段错误 [英] Same field error in aggregation _id

查看:123
本文介绍了聚合_id中存在相同的字段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中有一个这样的格式的集合:

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
            }
        }
    ]
}

我使用spring-data在该集合中进行查询,而我的查询就像这样:

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")
                    );

但是isemriSebebiisemriKaynaginame字段具有名为name的相同字段.当我映射到对象时,aggregationResult会给出错误'Same field error' 我该怎么做才能避免此错误?因为我所有该类的嵌入式文档都具有名为name的相同字段.

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.

推荐答案

不是最清晰的记录点.您需要使用Fields对象的其他表示形式来正确定义您的多字段条件:

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")
);

这使用的是Fields.field定义,该定义具有一个名称"和一个目标",以便正确地解释它.使用纯字符串将导致默认操作,即剥离所有内容,直到字段名称中的最后一个点"为止,并且根据您的错误,两者都是同一回事.以这种形式指定可以告诉构建者如何正确处理它.

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.

基本上将 $group 序列化为:

Which basically serializes the $group as:

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

您想要的是什么.

这篇关于聚合_id中存在相同的字段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆