0

I'm trying to learn MongoDB, and I'm running into issues. First one, I'd like to for example search for every embedded document that has the field "vuosi" equal less than 2019. I tried the following:

db.yhtyeet.find( { "levyt": { vuosi: { $lt: 2019 } } } );

Here's how the Database .json file reads out:

{
  "nimi": "Koodaajaorkesteri",
  "levyt": [
    {
      "nimi": "Koodinvääntäjä",
      "vuosi": 2013,
      "genret": [1, 2],
      "kappaleet": [
        {
          "nimi": "Koodaa kuin mies",
          "sav": "Tommi",
          "san": "Tommi"
        },
        {
          "nimi": "Ikuinen silmukka",
          "sav": "Jakke",
          "san": "Jakke"
        },
        {
          "nimi": "Iisi ostaa verkosta",
          "sav": "Jakke",
          "san": "Niko"
        }
      ]
    },
    {
      "nimi": "Syntax Error",
      "vuosi": 2019,
      "genre": [1, 2],
      "kappaleet": [
        {
          "nimi": "Pistetään pistetään omenaa poskeen",
          "sav": "Jakke",
          "san": "Jakke"
        },
        {
          "nimi": "Testaajahumppa",
          "sav": "Niko",
          "san": "Jakke"
        }
      ]
    }
  ]
}

Another issue: I'm trying to import multiple .json files into MongoDB. Firstly, is it correct to separate them with a comma?

$ docker exec -i nosql_mongo_1 mongoimport -u root -ppassword --authenticationDatabase=Admin -d ikk -c < ikk.json, ikkkayttaja.json, ikkroolit.json, ikktagit.json

The issue I'm getting is ikk.json,: No such file or directory even though it does exist and I'm in the correct folder. Is there something wrong with the file itself regarding syntax? Here are its contents:

{
    nimi: "PingPong",
    luokka: "Peli",
    tagit: [2, 3],
    tekijat: [
        {
            nimi: "Keijo-Antero Kuronen",
            rooli: [1, 2]
        },
        {
            nimi: "Sanna Suomalainen",
            rooli: [3, 4]
        }
    ],
    katsottu: 100,
    ladattu: 21
}
{
    nimi: "Tiko-Karate",
    luokka: "Peli",
    tagit: [1, 3, 4],
    tekijat: [
        {
            nimi: "Raimo Ruuskanen",
            rooli: [1, 3]
        },
        {
            nimi: "Pentti Puustinen,
            rooli: [3, 4]
        }
    ],
    katsottu: 55,
    ladattu: 23
}
{
    nimi: "Naamakirja",
    luokka: "Hyotysovellus",
    tagit: [5, 6],
    tekijat: [
        {
            nimi: "Ilmari Immonen-Innanen",
            rooli: [1, 5]
        },
        {
            nimi: "Raija-Irmeli Rantanen",
            rooli: [1, 3, 4]
        }
    ],
    katsottu: 133,
    ladattu: 16
}
mntblnk
  • 77
  • 7

1 Answers1

0

To answer your first question, you can query the nested document by referencing the first field and the nested one with a dot notation "field.nestedField" (see documentation):

db.inventory.find( { "levyt.vuosi": { $lt: 2019 } } );

To answer your second question, you cannot import multiple collections with mongoimport (see documentation). You can do a workaround with script commands to call mongoimport in a loop. This question can help : Import more than 1 json file using mongoimport.

XavierBrt
  • 1,179
  • 8
  • 13