聚合_id中的相同字段错误 [英] Same field error in aggregation _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")
);
但是 isemriSebebi
和 isemriKaynagi
的 name
字段具有相同的字段名为 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屋!