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.