How to query document with array and return array items as new documents

Hello, I am new and I am now entering the mongodb world with small steps … I have a question, I cannot understand how to do this query

document

{ "browserName": "firefox",
  "host": "xxx4551",
  "tests": [
      {"description": "Execute 01", "userStoryId": 1},
      {"description": "Execute 02", "userStoryId": 2},
      {"description": "Execute 03", "userStoryId": 3}
   ]
}

I would like an output of this kind

browserName, host, description, userStoryId
firefox, xxx4551, Execute 01.1
firefox, xxx4551, Execute 02.2
firefox, xxx4551, Execute 03.3

it’s possible?

Could you also indicate some courses?

Thank you

MongoDB has a really good online learning university call MongoDB university I would suggest starting with M001 MongoDB Basics which will help you understand the basics of MongoDB including basic querying.

As for the query I’m not sure what you are trying to query. Are you just trying to format the output of a find?

yes I have to format the output, display the browserName, host columns and the single entry of the tests array, consisting of description and userStoryId.

create a report that gives me this formatting

BrowserName | HOSTS | Description | UserStoryID

I don’t know what language you’re using, but it returns a JSON result that you can parse.

Example in javascript:

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

console.log(obj.result);
// expected output: true

console.log(obj.count);
// expected output: 42

You do not need to parse.

The API gives you an object so you simply access the members like any other JS object (or python list and dictionary).

From your example and in JS the following code:

formatted = `${document.browserName} | ${document.host}`
console.log( formatted )

will print the following line:

firefox | xxx4551

1 Like

no, maybe I explained myself wrong … from a document in mongoDB, I need to create a report that has this output

browserName host description userStoryId
firefox xxx4551 Execute 01 1
firefox xxx4551 Execute 02 2
firefox xxx4551 Execute 03 3

via a mongoDB query

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.