Список списков, не записывающих должным образом в базу данных mongo с использованием mongolite в R - PullRequest
0 голосов
/ 09 января 2019

dput() в моем списке довольно большой, однако я хотел убедиться, что у меня есть воспроизводимый пример для этой проблемы.

У меня есть объект вложенного списка (списков) в R, который я получил, сделав вызов API для спортивного API, к которому у меня есть доступ. Ниже приведен пример этого с данными о 33 различных конференциях по баскетболу в колледже. (из-за размера данных отображаются только 2/33 конференций):

> dput(cbb.conferences[1:2])
list(list(id = "04d5255d-b2dc-43df-9fa8-d296b0f8ccd7", name = "Pacific 12", 
    alias = "PAC12", teams = list(list(id = "0d037a5d-827a-44dd-8b70-57603d671d5d", 
        name = "Utes", market = "Utah", alias = "UTAH", venue = list(
            id = "c06cdbce-91ba-4306-b31c-97df5cbf2515", name = "Jon M. Huntsman Center", 
            capacity = 15000L, address = "1825 East South Campus Drive", 
            city = "Salt Lake City", state = "UT", zip = "84112", 
            country = "USA")), list(id = "1da70895-f77f-44ef-b216-d63c02e696eb", 
        name = "Ducks", market = "Oregon", alias = "ORE", venue = list(
            id = "591ff737-8ad6-4a23-9de1-3721d5c4d8a1", name = "Matthew Knight Arena", 
            capacity = 12364L, address = "1776 East 13th Street", 
            city = "Eugene", state = "OR", zip = "97401", country = "USA")), 
        list(id = "2d4f0015-adb4-4877-8c15-4a6eed7eed03", name = "Cougars", 
            market = "Washington State", alias = "WSU", venue = list(
                id = "cccd8a1b-21ba-40a5-b197-7561bc74cacb", 
                name = "Beasley Coliseum", capacity = 11671L, 
                address = "NE North Fairway Road", city = "Pullman", 
                state = "WA", zip = "99163", country = "USA")), 
        list(id = "3a000455-de7c-4ca8-880e-abdce7f21da9", name = "Trojans", 
            market = "USC", alias = "USC", venue = list(id = "d03346fb-8618-4ad7-bbd8-1554a003c472", 
                name = "Galen Center", capacity = 10258L, address = "3400 South Figueroa Street", 
                city = "Los Angeles", state = "CA", zip = "90089", 
                country = "USA")), list(id = "532d3874-b4b3-4c5c-acc6-749a6db26c8f", 
            name = "Beavers", market = "Oregon State", alias = "ORST", 
            venue = list(id = "02ccdcd1-9754-4816-9c68-2f7b9a27f844", 
                name = "Gill Coliseum", capacity = 9604L, address = "660 Southwest 26th Street", 
                city = "Corvallis", state = "OR", zip = "97331", 
                country = "USA")), list(id = "683ab61f-546f-44da-b085-c3a5740554aa", 
            name = "Cardinal", market = "Stanford", alias = "STAN", 
            venue = list(id = "9fe460a5-6d1e-4e27-a152-f867e2d6d099", 
                name = "Maples Pavilion", capacity = 7392L, address = "655 Campus Drive", 
                city = "Stanford", state = "CA", zip = "94305", 
                country = "USA")), list(id = "9b166a3f-e64b-4825-bb6b-92c6f0418263", 
            name = "Wildcats", market = "Arizona", alias = "ARIZ", 
            venue = list(id = "60c78748-ee0e-4764-9c8c-141e876d2abf", 
                name = "McKale Center", capacity = 14644L, address = "1 National Championship Drive", 
                city = "Tuscon", state = "AZ", zip = "85721", 
                country = "USA")), list(id = "9fccbf28-2858-4263-821c-fdefb3c7efa3", 
            name = "Buffaloes", market = "Colorado", alias = "COLO", 
            venue = list(id = "9adf9df2-fc71-4c24-8ddf-ad77f4a935f8", 
                name = "Coors Events Center", capacity = 11064L, 
                address = "Kittredge Loop Road", city = "Boulder", 
                state = "CO", zip = "80302", country = "USA")), 
        list(id = "aacdc78d-b7fa-48f6-9686-2fdb8a95030e", name = "Golden Bears", 
            market = "California", alias = "CAL", venue = list(
                id = "bf0a9070-8049-41c9-9a4e-dfa0dcf6ad07", 
                name = "Haas Pavilion", capacity = 11877L, address = "14 Frank Schlessinger Way", 
                city = "Berkeley", state = "CA", zip = "94720", 
                country = "USA")), list(id = "ad4bc983-8d2e-4e6f-a8f9-80840a786c64", 
            name = "Sun Devils", market = "Arizona State", alias = "ASU", 
            venue = list(id = "1cfb6fcd-84c0-464a-b9bb-7ce687500dbc", 
                name = "Wells Fargo Arena", capacity = 10754L, 
                address = "600 East Veterans Way", city = "Tempe", 
                state = "AZ", zip = "85287", country = "USA")), 
        list(id = "e52c9644-717a-46f4-bf16-aeca000b3b44", name = "Huskies", 
            market = "Washington", alias = "WASH", venue = list(
                id = "afe57543-ac2b-4b38-9f45-1b73ff001cc2", 
                name = "Alaska Airlines Arena at Hec Edmundson Pavilion", 
                capacity = 10000L, address = "3870 Mountlake Boulevard NE", 
                city = "Seattle", state = "WA", zip = "98195", 
                country = "USA")), list(id = "ec0d6b67-4b16-4b50-92b2-1a651dae6b0f", 
            name = "Bruins", market = "UCLA", alias = "UCLA", 
            venue = list(id = "e7547c99-c087-4245-b039-19dc1f8df1b0", 
                name = "Pauley Pavilion", capacity = 13800L, 
                address = "301 Westwood Plaza", city = "Los Angeles", 
                state = "CA", zip = "90095", country = "USA")))), 
    list(id = "0f3991a1-8c5f-4a8e-8fab-d0ebbb21c35c", name = "Ohio Valley", 
        alias = "OVC", teams = list(list(id = "13358462-e05b-4449-8688-90c4622cdde8", 
            name = "Gamecocks", market = "Jacksonville State", 
            alias = "JVST", venue = list(id = "7e440eb5-7e21-4f97-8f88-c39852009991", 
                name = "Pete Mathews Coliseum", capacity = 5300L, 
                address = "700 N Pelham Road", city = "Jacksonville", 
                state = "AL", zip = "36265", country = "USA")), 
            list(id = "23fec4e7-108f-4569-85d1-bef2f794e925", 
                name = "Panthers", market = "Eastern Illinois", 
                alias = "EIU", venue = list(id = "98445659-5230-469d-95fd-471ab579d2ef", 
                  name = "Lantz Arena", capacity = 5400L, address = "600 Lincoln Avenue", 
                  city = "Charleston", state = "IL", zip = "61920", 
                  country = "USA")), list(id = "6a7083ab-1832-48c0-9168-427b35adbcde", 
                name = "Redhawks", market = "Southeast Missouri State", 
                alias = "SEMO", venue = list(id = "1d7f1e41-996f-421c-bee9-d98388be3e27", 
                  name = "Show Me Center", capacity = 6972L, 
                  address = "1333 North Sprigg Street", city = "Cape Girardeau", 
                  state = "MO", zip = "63701", country = "USA")), 
            list(id = "77a69fb0-1355-4342-ac09-b4cc7949d95e", 
                name = "Racers", market = "Murray State", alias = "MURR", 
                venue = list(id = "0889bc9d-7348-478e-801c-8c71a2417bbb", 
                  name = "CFSB Center", capacity = 8602L, address = "1401 State Route 121 N", 
                  city = "Murray", state = "KY", zip = "42071", 
                  country = "USA")), list(id = "87721c44-53a2-47aa-9b3a-0f1c99b0f328", 
                name = "Tigers", market = "Tennessee State", 
                alias = "TNST", venue = list(id = "41300711-0d9a-4501-bda0-f2841fbdec2e", 
                  name = "Gentry Complex", capacity = 10500L, 
                  address = "3500 John A. Merritt Boulevard", 
                  city = "Nashville", state = "TN", zip = "37209", 
                  country = "USA")), list(id = "9febd5fa-982f-4f40-ad1d-8e49be96cf4f", 
                name = "Skyhawks", market = "Tennessee-Martin", 
                alias = "UTM", venue = list(id = "e9969453-033e-4fdd-99dc-5212240c8c9b", 
                  name = "Skyhawk Arena", capacity = 4800L, address = "554 University Street", 
                  city = "Martin", state = "TN", zip = "38238", 
                  country = "USA")), list(id = "a0a22502-0d84-440c-84af-1fbb070ad54c", 
                name = "Bruins", market = "Belmont", alias = "BEL", 
                venue = list(id = "8af089c8-7dc5-44a5-917c-fc0bb9040e0c", 
                  name = "Curb Event Center", capacity = 5085L, 
                  address = "2002 Belmont Boulevard", city = "Nashville", 
                  state = "TN", zip = "37212", country = "USA")), 
            list(id = "a0fdb660-a822-4337-b01d-31c4d2c99c8a", 
                name = "Governors", market = "Austin Peay", alias = "PEAY", 
                venue = list(id = "61d49f1f-05d1-4f0a-89d9-c2aaf51812ea", 
                  name = "Winfield Dunn Center", capacity = 7257L, 
                  address = "601 Drane Street", city = "Clarksville", 
                  state = "TN", zip = "37040", country = "USA")), 
            list(id = "b7747a03-c85b-4de6-94bf-1b5b68553248", 
                name = "Eagles", market = "Morehead State", alias = "MORE", 
                venue = list(id = "4ff71c66-7ac0-4726-a1b5-9f4706933b72", 
                  name = "Ellis T. Johnson Arena", capacity = 6500L, 
                  address = "111 Playforth Place", city = "Morehead", 
                  state = "KY", zip = "40351", country = "USA")), 
            list(id = "c206712f-2298-4279-b76c-834ac42809b9", 
                name = "Cougars", market = "SIU-Edwardsville", 
                alias = "SIUE", venue = list(id = "4b6b39a9-d8dc-4af0-b879-03865bd4808a", 
                  name = "Sam M. Vadalabene Center", capacity = 4000L, 
                  address = "35 Circle Drive", city = "Edwardsville", 
                  state = "IL", zip = "62025", country = "USA")), 
            list(id = "e48e3dd5-721c-4d96-8ff4-790fe2597bfd", 
                name = "Colonels", market = "Eastern Kentucky", 
                alias = "EKY", venue = list(id = "730eda92-17aa-479c-aa90-05864f00d132", 
                  name = "Alumni Coliseum McBrayer Arena", capacity = 6500L, 
                  address = "521 Lancaster Avenue", city = "Richmond", 
                  state = "KY", zip = "40475", country = "USA")), 
            list(id = "fe037cba-25d9-42f5-a461-61d7102c17de", 
                name = "Golden Eagles", market = "Tennessee Tech", 
                alias = "TNTC", venue = list(id = "10268640-b52c-4959-ac59-7ec0172561f5", 
                  name = "Hooper Eblen Arena", capacity = 9280L, 
                  address = "1100 McGee Boulevard", city = "Cookeville", 
                  state = "TN", zip = "38501", country = "USA")))))

Вот общее представление о том, что содержат списки верхнего уровня (в моем полном списке длиной 33):

> unlist(lapply(cbb.conferences, FUN = function(x) return(x$name)))
 [1] "Pacific 12"              "Ohio Valley"             "Missouri Valley"         "Big 12"                  "Sun Belt"               
 [6] "Independents (DI)"       "Big Sky"                 "American Athletic"       "Southern"                "Summit League"          
[11] "Big West"                "Conference USA"          "Western Athletic"        "Southland"               "Southwestern Athletic"  
[16] "Mid Eastern Athletic"    "Atlantic Coast"          "Atlantic Sun"            "Patriot League"          "Big South"              
[21] "Horizon"                 "Mountain West"           "Southeastern"            "Mid-American"            "Big Ten"                
[26] "America East"            "Northeast"               "Ivy"                     "Colonial"                "West Coast"             
[31] "Big East"                "Atlantic 10"             "Metro Atlantic Athletic"

> unlist(lapply(cbb.conferences, FUN = function(x) return(x$alias)))
 [1] "PAC12"     "OVC"       "MVC"       "BIG12"     "SUNBELT"   "DI-IND"    "BIGSKY"    "AAC"       "SOUTHERN"  "SUMMIT"    "BIGWEST"  
[12] "CUSA"      "WAC"       "SOUTHLAND" "SWAC"      "MEAC"      "ACC"       "AS"        "PATRIOT"   "BIGSOUTH"  "HORIZON"   "MWC"      
[23] "SEC"       "MAC"       "BIG10"     "AE"        "NE"        "IVY"       "COLONIAL"  "WCC"       "BIGEAST"   "A10"       "MAAC"     

> unlist(lapply(cbb.conferences, FUN = function(x) return(x$id)))
 [1] "04d5255d-b2dc-43df-9fa8-d296b0f8ccd7" "0f3991a1-8c5f-4a8e-8fab-d0ebbb21c35c" "1d9219c1-696c-42a2-b1be-e8b438795521"
 [4] "2853cf4d-6d62-4ec6-8e2c-d69f7a01a557" "302c99fe-6b0a-40ec-8ee7-f15a0355b7b5" "30ba44da-12b9-4d16-94f8-791c9ebb12b2"
 [7] "3126fb79-47a7-4a57-b065-8b9d1bdb660d" "3b6a48d8-1f9c-484f-8ed0-ef0a540a0efe" "468829d1-8886-4c14-91d1-e6441673b2f6"
[10] "49665f99-1a5f-434f-b841-ddde229f0eb9" "5f56da49-5d81-43c3-8652-8b8da40c907f" "6902bb03-02f7-4da4-8261-de5fd2cbd011"
[13] "7265822b-247a-4991-ae8c-0e6f33f6122b" "763ae15d-32b0-476d-aa12-0c7f4a3cd423" "7be69d78-bd13-4bfa-ae33-a6e48131a28f"
[16] "852139a9-389c-4eca-9760-ec13f014ca8d" "88368ebb-01fb-44d5-a6c6-3e7d46bb3ab7" "888a4792-c632-447f-a8fd-fb0b6f04ce8e"
[19] "88acc3d7-e5a1-4751-864e-606223943a62" "906a1ec5-c786-4b14-9f7a-30e0c0f53255" "92db2b30-c5ac-41e9-ba18-59f3c2a0dd0b"
[22] "93a776e4-d390-48e1-95bb-74945457366a" "9ed9d01e-977c-4ba7-ac7d-64035039461c" "9f1cd1d6-fb03-493d-b278-b74ce25cc57b"
[25] "a30fe8ff-82d2-4521-bc8d-e08e6a9dbb52" "b11c69c8-e2cc-4a36-bd3b-80cdf91fef57" "b71a249a-9cb8-4bfd-8971-e7ca7bed6700"
[28] "c2d16439-b791-4f9b-84d9-80cf457a0676" "c56db9d1-604f-40ad-8a7f-c3b8abd16fca" "c664ceee-1dc0-4743-a6d8-11fbdfb87f61"
[31] "d07bc93e-c84c-44a9-a99d-c213bd0014d6" "db136c00-f45b-4af6-bc7d-ffff216f2e5d" "ec86f5b1-17c7-470f-9bcd-b86f61b76945"

... ... Этот список списков не имеет имен, поэтому я добавил их в список. Затем я записываю список в коллекцию mongo db, используя mongolite:

library(mongolite)
names(cbb.conferences) <- unlist(lapply(cbb.conferences, FUN = function(x) return(x$alias)))
con <- mongolite::mongo(collection = 'mycol', db = 'mydb', url = 'myurl')
con$insert(cbb.conferences)

Используя Robo3T в качестве графического интерфейса на моем Mac для просмотра таблицы, я замечаю, что она записана в мою базу данных в следующем формате:

enter image description here Однако, это не то, что я хочу . Коллекция была создана так:

  • один объект верхнего уровня с 33 вложенными объектами конференции внутри
  • для каждого из этих 33 вложенных объектов, 4 вложенных объекта (идентификатор, имя, псевдоним, команды)
  • id, name и alias - это именованные массивы length-1 с одним объектом в качестве значения.

Однако мои намерения для коллекции заключались в том, чтобы она была структурирована следующим образом:

  • массив верхнего уровня с 33 вложенными объектами конференции внутри
  • для каждого из этих 33 вложенных объектов, 3 значения (id, name, alias) и 1 вложенный список (команды)

Кажется, что способ создания коллекции чрезмерно вложен , и мои намерения для коллекции приведут к лучшей структурированности коллекции. Я напишу пример того, на что похож результат, на который я иду. Любая помощь с этим будет принята с благодарностью!

РЕДАКТИРОВАТЬ: что-то вроде этого:

[
  PAC12: {
    id: "04d5255d-b2dc-43df-9fa8-d296b0f8ccd7",
    name: "Pacific 12",
    alias: "PAC12",
    teams: [
      { name: ..., id:, venue: ... },
      { name: ..., id:, venue: ... },
      ...
    ]
  }, 
  OVC: {...},
  MVC: {...},
  ... 
]

Редактировать 2: Правильно ли я пытаюсь заставить это изменение в структуре данных при записи в мою базу данных? Наличие объектов из массивов значений длины 1 кажется чрезмерным по сравнению с простым массивом объектов значений. Хотелось бы, чтобы было проще писать с помощью монголита из R в базу данных монго и указывать эти предпочтения в структуре коллекции.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...