Closed
Description
Prerequisites
- I have written a descriptive issue title
Mongoose version
8.7.1
Node.js version
20.x
MongoDB version
6.9
Operating system
None
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
Last night, I upgraded my mongodb and mongoose from versions 5.8.1 to 6.9 and. 7.4.5 to 8.7.1 respectively.
I tested on my local. Everything worked fine. I promoted the changes to test env - AWS amplify - things were fine. Then, I moved the changes to production.
My db is hosted on mongo atlas serverless cluster.
On production, sometimes same queries will work fine but other times give timeout error with 10000ms. I mean same queries behaving differently.
I have been scratching my head because both test and prod env are exactly similar except that prod is connected to my domain name.
Please assist if someone has knowledge about this.
sharing my connection code and one query that fails/passes abruptly.
import { ServerApiVersion } from "mongodb";
import mongoose from "mongoose";
const uri = process.env.MONGODB_URI as string;
const connection: {
isConnected?: boolean;
} = {};
const dbConnect = async () => {
if (connection.isConnected) return;
mongoose.set("strictQuery", false);
const db = await mongoose.connect(uri);
connection.isConnected = Boolean(db.connections[0].readyState);
};
export default dbConnect;
export async function GET(req: Request) {
try {
await apiHandler(req, "");
const countries: any = await Countries.find({}).exec();
return NextResponse.json(countries, { status: 200 });
} catch (error: any) {
console.log("wb saas: countries");
console.log(
typeof error !== "string"
? error.message
? error.message
: (error || {}).toString()
: error
);
return errorHandler(error);
}
}