MongoDB query find

Question

Hello I am new in MongoDB. I have 6 Objects like this:

{
    "_id" : ObjectId("5957a8eee0f3890011b64b99"),
    "name" : "Interní šetření spokojenosti zaměstnanců",
    "client" : ",
    "version" : "",
    "date" : ISODate(""),
    "ip" : "",
    "answers" : {
        "doporucil_produkt" : "ano",
        "vystupovani_firmy" : "ano",
        "vim_vizi" : "ne",
        "uvazuje_ozmene_proc_zustat" : "lepšie finančné ohodnotenie, zlepšenie tímovej práce",
        "uvazuje_ozmene_proc" : "vzdialenosť od bydliska",
        "uvazuje_ozmene" : "ano",
        "rozvoj_skoleni_chci" : "jazykový kurz",
        "rozvoj_skoleni" : "",
        "rozvoj_iniciativa" : "ano",
        "rozvoj_naslouchaji" : "ano",
        "rozvoj_vimco" : "ne",
        "rozvoj_rust_moznosti" : "5",
        "rozvoj_rust" : "[\"Aktivní participace na strategii firmy\",\"Zvyšování odbornosti\"]",
        "rozvoj_zustanu" : "ano",
        "spokojenostsnaplni" : "5",
        "rozumimnaplni" : "ano",
        "doporucilibyste" : "4",
        "ohodnoceni_prostredi_cozmenit" : "možnosť využitia bazénu a tenisových kurtov aj pre zamestnancov",
        "ohodnoceni_prostredi" : "dobrý kávovar, príjemné prostredie",
        "ohodnoceni_benefity_zbytecne_dalsi" : "",
        "ohodnoceni_benefity_chci_dalsi" : "lekárske prehliadky - v jeden deň\r\ndovolenka navyše",
        "ohodnoceni_benefity_dalsi" : "",
        "ohodnoceni_benefity" : "[\"benefits_homeoffice\",\"benefits_flexibleworkinghours\",\"benefits_car\"]",
        "ohodnoceni_benefity_spokojenost" : "ano",
        "ohodnoceni_proc_nepokojenost" : "",
        "ohodnoceni_spokojenost" : "ano",
        "ohodnoceni_proc_podhoceni" : "300€",
        "ohodnoceni_dle_vykonu" : "ne",
        "leadership_podpora_life" : "5",
        "leadership_podpora_work" : "4",
        "leadership_realnepozadavky" : "ano",
        "leadership_ocekavani" : "ano",
        "leadership_komunikace" : "4",
        "leadership_minus" : "viesť kolektív k tímovosti",
        "leadership_plus" : "priamosť, ústretovosť",
        "leadership_score" : "5",
        "leadership_komunikace_firma" : "3",
        "tym_spoluprace" : "4",
        "tym_offwork" : "ne",
        "leadership_komunikace_tym" : "3",
        "tym_kolegove" : "ne",
        "dreamjob" : "ne",
        "spokojenost" : "4",
        "workExperience" : "many",
        "quitYear" : "",
        "workedYears" : "0,6",
        "status" : "current",
        "position" : "manažér predaja",
        "department" : "obchod",
           },
    "__v" : 0
}

I need js function or query for seeing every values under the "answers" in one row for example - obchod, manazer predaja, current,...

I used this :

db.getCollection('surveys').find().forEach(function(myDoc) { print( "user: " +myDoc.answers.department, myDoc.answers.position, myDoc.answers.status,

But I dont want to write all "answers" subsequences.

Any sugestions?

thank you very much Stuli


Show source
| find   | mongodb   2017-09-26 23:09 1 Answers

Answers to MongoDB query find ( 1 )

  1. 2017-09-27 02:09

    You can use Object.keys to get all the keys of the object in an array. Then you can map over them to return the values. I think this function will do what you're looking for:

    db.surveys.find().forEach(function(doc) { 
      var answers = Object.keys(doc.answers).map(function(e) { return ' ' + e });
      print('user:' + answers);
    })
    

    Just so you know, you can use Object.values in the latest versions of JavaScript. But it's not available in MongoDB 3.4's JavaScript engine.

Leave a reply to - MongoDB query find

◀ Go back