Я новичок в mongoDB (только сегодня!). Я выполняю следующий запрос (к базе данных о насилии с применением оружия), который объединит массив строк в одну строку, разделенную пробелом. Связанная строка должна называться incidentString
, а массив строк - misc.incident_characteristics
.
db.fin.aggregate([
{ "$addFields": {
"incidentString": {
"$reduce": {
"input": "$misc.incident_characteristics",
"initialValue": "",
"in": {
"$cond": {
"if": { "$eq": [ { "$indexOfArray": [ "$values", "$$this" ] }, 0 ] },
"then": { "$concat": [ "$$value", "$$this" ] },
"else": { "$concat": [ "$$value", " ", "$$this" ] }
}
}
}
}
}}
])
Хотя кажется, что она работает для многих строк в моей базе данных, в конечном итоге она возвращает ошибку, которую я не могу понять, как интерпретировать. Я покажу вывод ниже. Обратите внимание, что incidentString
успешно создан для некоторых записей в базе данных, но я показываю только одну в выходных данных ниже.
{
"_id" : 488992,
"date" : ISODate("2013-06-15T00:00:00Z"),
"location_info" : {
"state" : "Rhode Island",
"city_or_county" : "Providence",
"address" : "256 Hartford Ave",
"latitude" : 41.8173,
"longitude" : -71.4516
},
"casualties" : {
"n_killed" : 1,
"n_injured" : 3
},
"sources" : {
"incident_url" : "http://www.gunviolencearchive.org/incident/488992",
"source_url" : "http://wpri.com/2014/12/19/luis-fat-boy-gonzalez-to-learn-punishment/",
"incident_url_fields_missing" : false,
"sources" : [
"http://turnto10.com/archive/shooting-in-providence-leaves-3-injured-and-a-12-year-old-girl-dead",
"http://www.providencejournal.com/article/20141118/News/311189975",
"http://patch.com/rhode-island/cranston/5-indicted-for-killing-of-former-nk-girl-cranston",
"http://wpri.com/2014/12/19/luis-fat-boy-gonzalez-to-learn-punishment/"
]
},
"law_info" : {
"congressional_district" : 2,
"state_house_district" : 9,
"state_senate_district" : 7
},
"gun_info" : {
"gun_attr" : [
{
"_id" : 0,
"gun_stolen" : "Unknown",
"gun_type" : "Unknown"
}
],
"n_guns_involved" : 1
},
"misc" : {
"incident_characteristics" : [
"Shot - Wounded/Injured",
"Shot - Dead (murder, accidental, suicide)",
"Mass Shooting (4+ victims injured or killed excluding the subject/suspect/perpetrator, one location)",
"Gang involvement",
"Possession of gun by felon or prohibited person"
],
"notes" : "ms; 1 killed, 3 inj at graduation party by rival gang. All 5 perps guilty and sentenced."
},
"participant_info" : [
{
"_id" : 0,
"participant_age" : "12",
"participant_age_group" : "Teen 12-17",
"participant_gender" : "Female",
"participant_name" : "Aynis Vargas",
"participant_status" : "Killed",
"participant_type" : "Victim"
},
{
"_id" : 1,
"participant_age" : "44",
"participant_age_group" : "Adult 18+",
"participant_gender" : "Female",
"participant_name" : "Vilma Tineo",
"participant_status" : "Injured",
"participant_type" : "Victim"
},
{
"_id" : 2,
"participant_age" : "23",
"participant_age_group" : "Adult 18+",
"participant_name" : "Elaine Devargas",
"participant_status" : "Injured",
"participant_type" : "Victim"
},
{
"_id" : 3,
"participant_age" : "33",
"participant_age_group" : "Adult 18+",
"participant_gender" : "Female",
"participant_name" : "Eugelyn Cabera-Martinez",
"participant_status" : "Injured",
"participant_type" : "Victim"
},
{
"_id" : 4,
"participant_age" : "21",
"participant_age_group" : "Adult 18+",
"participant_gender" : "Male",
"participant_name" : "Branden Castro",
"participant_status" : "Unharmed, Arrested",
"participant_type" : "Subject-Suspect"
},
{
"_id" : 5,
"participant_age" : "20",
"participant_age_group" : "Adult 18+",
"participant_gender" : "Male",
"participant_name" : "Ricardo Vasquez",
"participant_status" : "Unharmed, Arrested",
"participant_type" : "Subject-Suspect"
},
{
"_id" : 6,
"participant_age" : "23",
"participant_age_group" : "Adult 18+",
"participant_gender" : "Male",
"participant_name" : "Luis Gonzales",
"participant_status" : "Unharmed, Arrested",
"participant_type" : "Subject-Suspect"
},
{
"_id" : 7,
"participant_age" : "19",
"participant_age_group" : "Adult 18+",
"participant_gender" : "Male",
"participant_name" : "Angel Valerio",
"participant_status" : "Unharmed, Arrested",
"participant_type" : "Subject-Suspect"
},
{
"_id" : 8,
"participant_age" : "17",
"participant_age_group" : "Teen 12-17",
"participant_status" : "Unharmed, Arrested",
"participant_type" : "Subject-Suspect"
}
],
"incidentString" : " Shot - Wounded/Injured Shot - Dead (murder, accidental, suicide) Mass Shooting (4+ victims injured or killed excluding the subject/suspect/perpetrator, one location) Gang involvement Possession of gun by felon or prohibited person"
}
Error: command failed: {
"ok" : 0,
"errmsg" : "$reduce requires that 'input' be an array, found: \"Shot - Dead (murder, accidental, suicide)\"",
"code" : 40080,
"codeName" : "Location40080"
} : getMore command failed: {
"ok" : 0,
"errmsg" : "$reduce requires that 'input' be an array, found: \"Shot - Dead (murder, accidental, suicide)\"",
"code" : 40080,
"codeName" : "Location40080"
}
Из рассмотрения ошибки ("$ lower требует, чтобы" input "был массив ... "), кажется misc.incident_characteristics
не является ли массив строк для чего-то в Location40080? Как мне go исправить это?