Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
Mongoose Work only first time but doesn't works second time onwards

Below is the Code:

import mongoose from "mongoose"
const testingScript = async () => {
    for (let i = 0; i < 10; i++) {
      console.log("Index", i);
      mongoose.connect('URL');
      
      var connection = mongoose.connection;
      
      connection.on('error', console.error.bind(console, 'connection error:'));
      connection.once('open', function () {
      
          connection.db.collection("test", function(err, collection){
              collection.find({abc: "12345"}).toArray(function(err, data){
                  console.log(data); // it will print your collection data
              })
          });
      
      });

    }
  };

  testingScript()

Below is code for Mongoose Schema that is saved in DB.

import { Schema, model, Document, Types, Mongoose } from "mongoose";

const userSchema = new Schema(
  {
    name: {type: String},
    email: {type: String},
    deletedAt: { type: Date, default: null },
    schema_version: { type: Number, default: 1 },
  },
  {
    timestamps: true,
  }
);


export interface IUser extends Document {
  _id: string;
  name: string;
  email: string;
  createdAt: Date;
  updatedAt?: Date;
  deletedAt?: Date;
  schema_version: number;
}
const user = model<IUser>("User", userSchema);

For the first POST request, it will get all the records but afterward, it will get only one record. Moreover after the second request, even GET doesn't return anything from DB.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!