Here is a method for retrieving all fields within embedded documents at the same level in MongoDB

Thanks to the magic of embedded documents, we can avoid complicated join operations.

Yet, I find myself needing to extract all fields on a single level for creating lists or reports.

Is there a straightforward way to achieve this?

For example; I wish to generate a report including all the mentioned fields. In order to do so, I must flatten the JSON data structure.

{
   "_id": "1",
   "date" : "2014-07-30 02:00",
   "personnel_id": "14",
   "personnel_name": "xxxx Stackton",
   "personnel_city": "nevercity",
   "personnel_province": "neverland",
   "customer_id": "473",
   "customer_name": "xxxx Jordan",
   "payment_method": "cash",
   "payment_amount": "67.40"
}

Answer №1

To loop through the keys of an object, you can use a method similar to how you would with a JSON object.

For example, the code below will display the keys name and age:

var person = { name: 'Sarah', age: 29 };

for(property in person) {
  console.log(property);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can you delete using JavaScript by pressing the "Delete" key?

Is it possible to programmatically trigger the deletion of text on an HTML web page using JavaScript or jQuery? I am looking for a way to replicate the functionality of clicking the "Delete" button without requiring users to physically click on it. Inste ...

Creating Test Cases for Service Response Validation

I am currently attempting to unit test an API within my ngOnInit method. The method is responsible for making a call to the service in order to fetch details. If the details are not undefined, an array called 'shoeDataResponse' of type *shoeData ...

Querying MongoDB for nested documents

My MongoDB data structure is as follows: { "_id" : ObjectId("512c4c468c08631ff0dbb02c"), "ReferenceNumber" : "MongoDb", "Title" : "Shashi", "CreatedUserId" : "users/1027", "Content" : "s", "CustomerId" : " ...

Ways to expand the line capacity in the Mongo shell

Whenever I attempt to execute a lengthy line with numerous inserts in the mongo shell, I encounter ambiguous errors: 2020-07-13T15:59:21.264+0100 E QUERY [js] uncaught exception: SyntaxError: expected expression, got ']' : @(shell):1:0 I hav ...

Here's the proper method to execute this in JavaScript

Hello everyone, thank you for stopping by. I'm trying to figure out the correct way to achieve the following: <script language="javascript"> function flag(nation) { this.nation=nation; document.getElementById("flag").innerHTML="<img src=&ap ...

What is the best way to calculate the number of days between today's date and the end of the current month using Moment.js?

Here's the current code snippet I am working with: const moment = require('moment') const m = moment const currDay = m().format('D') const dayOfWeek = m().format('dddd') const daysInMonth = m().daysInM ...

What could be causing the error message "Error: Cannot modify headers after they are sent" to appear?

I am attempting to insert data into an MS SQL server when a JSON Data API POST request is made. var express = require('express'); var app = express(); var sql = require('mssql'); // Connection string parameters. var sqlConfig = { u ...

Can you clarify the typescript type of the Mutation list within the callback function of the mutation observer?

Can you provide the typescript data type of the variable mutationList in the callback function? const targetNode = document.getElementById("some-id"); const config = { attributes: true, childList: true, subtree: true }; const callback = (mutationList, ...

Trigger a random tune when hovering the mouse

I need help figuring out how to make a fixed image on my page trigger a random sound track when hovered over. The sound could be one of five different tracks. Here is the HTML for my image: <img src="images/Airplane.png" class="Airplane-photo"> Bel ...

Using conditional subscriptions in Meteor at the template level

When working with the same template in different routes using a conditional publication method, I encountered an issue where the data was not being subscribed to as expected. The console log returned an empty array. server/publication.js Meteor.publish(& ...

Why does starting up the Firebase emulators trigger the execution of one of my functions as well?

Upon running firebase emulators:start --only functions,firestore, the output I receive is as follows: $ firebase emulators:start --only functions,firestore i emulators: Starting emulators: functions, firestore ⚠ functions: The following emulators are ...

What is the best method for integrating JavaScript into my Express.js application that is also utilizing Socket.IO?

In order to synchronize time for my countdown project, I am utilizing the timesync package. server.js const app = require('express')(); const http = require('http').Server(app); const io = require('socket.io')(http); const po ...

Exploring the process of assigning custom images to individual items within arrays in React JS

Within my Json file, I have an array that details the materials of various foods. When displaying these on my recipe page, each item of material should have a corresponding image. However, with numerous food items and materials, implementing this purely le ...

Refreshing an Angular datatable using promises - reload directive

I am currently using angular-datatables along with promises and everything is working smoothly. I have various actions that can be performed on each record (using angular $http resource) such as changing a status or similar tasks. However, I find that I n ...

Guide to using a TypeScript interface in a JSON file with Visual Studio Code

Imagine having a TypeScript interface as follows: interface Settings { id?: number tag: string } Is there a way to ensure that all .json files within a specific directory adhere to these requirements? If VS Code does not offer this functionality, ...

Issues with displaying AngularJs directive template

Having an issue with my AngularJs directive. Everything works perfectly fine when I use the "template" attribute, but when I switch to using "templateURL", it stops working. Both the JavaScript file for the directive and the HTML file for the template are ...

Sorting and Deduplicating MongoDB Data with Reduce

I'm currently using the Reduce function to generate a combined String of fields from an array. For instance, suppose I have an array of subdocuments named children - and each individual child contains a name field. For example: [ {name:"Zak"}, {n ...

What is the process for retrieving information from a database in Django when the submit button is clicked?

Is there a way to retrieve database objects in HTML only when the submit button is clicked, instead of loading them automatically on page load? Currently, I have an AJAX script running on a form that displays text entered in a textbox onto the HTML when th ...

Issue with Angular custom toggle functionality not functioning properly on mobile devices

I've been working on creating a custom toggle feature in Angular. While everything runs smoothly on desktop, I'm encountering issues on mobile devices. On desktop, the toggle works by clicking and dragging to the right, whereas on mobile, it shou ...

What are the best practices for integrating Firebase authentication pre-made UI in Next JS?

As someone new to React and NextJS, I am eager to implement an authentication service for my NextJS web application in order to manage user accounts efficiently. I am particularly interested in utilizing a familiar user login solution that offers a compre ...