Storing JSON arrays in MongoDB becomes chaotic when using the .save() function from Mongoose

I'm faced with the challenge of storing a lengthy JSON array in a free MongoDB database collection.

To achieve this, I establish a connection to my database using mongoose and then utilize a for loop to iterate through each JSON object in the array. These objects are saved appropriately in the corresponding collection thanks to the save() method provided by mongoose. The entire process takes place within an application that runs on node.js and employs the express framework.

The code snippet below illustrates how I accomplish this task:

 const data = [
  {
    "numero": 1531,
    "annee": 2022,
    "date": "22/05/2022",
    "premier_cas": 10
  },
  {
    "numero": 1532,
    "annee": 2022,
    "date": "29/05/2022",
    "premier_cas": 15
  }
//THOUSANDS OF OBJECTS
];

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const db = {};
db.mongoose = mongoose;
db.result = require("./result.model");

const Result = db.result;

db.mongoose
  .connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then((client) => {
    console.log("Successfully connect to MongoDB.");
    addResultToCollection();
  })
  .catch(err => {
    console.error("Connection error", err);
    process.exit();
  });

function addResultToCollection() {
  Result.estimatedDocumentCount(async (err, count) => {
    if (!err && count === 0) {
      for (let i = 0; i < data.length; i++) {
        await createAndSaveOneResult(data[i], i);
      }
    } else if (count !== 0) {
      console.log("Results have already been created!");
    }
  });
}

async function createAndSaveOneResult(json, counter) {
  new Result({
    numero: json.numero,
    annee: json.annee,
    date: json.date,
    premier_cas: json.cas
  }).save(err => {
    if (err) {
      console.log("error", err);
    }
    console.log("Results %d has been created", counter);
  });
}

The model for my Result is defined as follows:

const mongoose = require('mongoose');

const Result = mongoose.model(
    "Result",
    (new mongoose.Schema({
        numero: {
            type: Number,
            required: 'This field is required'
        },
        annee: {
            type: Number,
            required: 'This field is required'
        },
        date: {
            type: String,
            required: 'This field is required'
        },
        premier_cas: {
            type: Number,
            required: 'This field is required'
        },
    })).index({ "$**": 'text' })
);

module.exports = Result;

Despite implementing measures like async/await logic and introducing delays with setTimeOut(), the issue persists wherein the objects are not being saved in the correct order both in the terminal logs and the MongoDB collection entries.

If anyone has insights or solutions to rectify this problem and ensure orderly saving of the array objects, your guidance would be greatly appreciated.

Answer №1

After some investigation, I discovered the root of my issue - it turns out I had been utilizing the incorrect mongoose method all along. The save() function is designed to store a single document, while insertMany() is used for saving multiple documents.

Here is the corrected code snippet:

const db = require("./models");
const Role = db.role;
const Result = db.result;
db.mongoose
  .connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then((client) => {
    console.log("Connection to MongoDB successful.");
    addResultToCollection();
  })
  .catch(err => {
    console.error("Unable to connect", err);
    process.exit();
  });
 
function addResultToCollection() {
  Result.estimatedDocumentCount(async (err, count) => {
    if (!err && count === 0) {
      Result.insertMany(data)
        .then(function () {
          console.log("Data successfully inserted")
        }).catch(function (error) {
          console.log(error)
        });
    } else if (count !== 0) {
      console.log("Results already exist in the database!");
    }
  });
}

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

Determine the overall sum following the modification of rows

Need assistance with calculating the Grand Total of all rows to display at the bottom of a table using JQuery 1.9. Below is the JavaScript code: <script language="javascript"> $(".add_to_total").on('change', function() { var total = 0; $( ...

Encountering connection timeout issues when trying to link MongoDB Atlas with Mongoose

I've been attempting to establish a connection to my MongoDB Atlas database using mongoose, but unfortunately, I keep encountering the following error: (node:2327) UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-abjwg.gcp.mongodb.n ...

Unravel the ReadableStream object in nextjs 13 api route

I am encountering an issue with my server-side code where a value I am sending is not being interpreted correctly. My project is utilizing the experimental App directory feature of NextJS. //src/app/api/auth/route.js export async function POST(req, res) { ...

Can I trust the security of my credentials if they are able to be logged in the console

While working with NextJS, I've encountered a challenge in applying server-side rendering methods like getStaticProps to maintain credentials on the server rather than exposing them to the client. Despite my attempts, I couldn't find a straightfo ...

After selecting a subitem, the side menu will automatically close

I designed a side menu that expands to reveal submenus. However, when I click on a subitem within a submenu, the entire list closes. The HTML, CSS, and JavaScript code all appear to be correct as the template is functioning properly. But as soon as I ins ...

The Laravel view is displaying on Chrome dev-tools instead of redirecting to the blade template

Hello amazing developers at stackoverflow, I'm currently working on a project focused on campaign management. The goal is to enable users to create, edit, and preview their campaigns before publishing them. To achieve this, I am utilizing MySQL and F ...

Check and validate the adjacent field whenever a change is detected in this field in Angular

Currently, I have a select menu with options and a text field that accepts numerical input. The text field needs to adhere to specific ranges based on the selection from the select menu, which is managed through custom validation. My dilemma lies in trigge ...

conditionally manipulate colspan attribute with AngularJS

Is there a way to dynamically change the value of the 'th' colspan attribute based on a condition in Angular? I have attempted the following code, but it doesn't seem to be working. Can someone point out what might be incorrect here? <t ...

Arranging arrays in Javascript within two dimensions

In my data set, I possess an array that contains tags along with their respective counts. tags_array[0] = tags; tags_array[1] = tags_count; My goal is to rearrange the arrays based on the tag counts to easily identify the most popular tags. ...

How can we bring in a function into a Vue component?

I'm currently facing an issue with importing a single function into my Vue component. To tackle this problem, I separated the function into its own js file: randomId.js: exports.randomId = () => //My function ... Within my Vue component, I attem ...

Optimizing jQuery Dialog for Different Window Sizes

I am currently developing a responsive website and facing a challenge. I need to implement a popup for window sizes smaller than 480px, but on desktop screens, the content should be visible without being inside the popup. I want to avoid duplicating the co ...

Add multiple images to various div containers

I am facing a challenge with my code as I am trying to upload and display two different files inside of two different divs using two buttons. Currently, my code is only working for one image. How can I modify the code to handle two images successfully? Any ...

Exploring the power of async/await in conjunction with loops

My JavaScript code is designed to extract content from HTML pages and perform a crawling operation. However, the issue arises with asynchronous execution due to a request function. I attempted to utilize Promises and async & await to address this probl ...

The Jquery Datatable fails to display the accurate number of rows based on the selections made in the dropdown

I am working on an ajax call that returns a table. In the success method, I am using $("#tableID").dataTable();. Although it shows paging and the number of rows in the dropdown, it is displaying all rows instead of only the number of rows selected in the d ...

Top method for creating 12 dynamic product pages using AngularJS with customized routing and templates

As someone who is still relatively new to AngularJS, I am looking for advice on how to properly structure the products section of my application. My main product page will display all 12 products with clickable links to individual product pages. Each indi ...

Invoke a Google Apps Script through AJAX that necessitates authentication

I am looking to access a Google Apps Script via AJAX that requires user authorization to send an email from their Gmail account. The challenge lies in the fact that I need to send the email from within the Google Apps Script. The call originates from my w ...

I am attempting to link my Firebase real-time database with Cloud Firestore, but I am encountering import errors in the process

I am currently working on enhancing the online functionality of my chat app by implementing a presence system using Firebase Realtime Database. Here is the code snippet that I have created for this purpose: db refers to Firestore and dbt refers to the Rea ...

Exclusively utilize optgroup multiple functionality within bootstrap-select

I'm currently utilizing https://github.com/snapappointments/bootstrap-select/ version 1.13.18 and I am in need of a solution where only optgroup options can have multiple selections. This means that if a non-optgroup option is selected, all other opti ...

How can I efficiently utilize the date picker feature in Angular JS for a smooth user experience?

I am new to AngularJS and attempting to implement a date picker. I initially tried using a basic jQuery UI date picker, but it did not function as expected. Can someone please provide me with some code that demonstrates the simplest way to achieve this in ...

JavaScript: Creating Custom IDs for Element Generation

I've been developing a jeopardy-style web application and I have a feature where users can create multiple teams with custom names. HTML <!--Score Boards--> <div id="teamBoards"> <div id="teams"> ...