Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.91k stars 3.83k forks source link

Issue while connecting to database (Bug) #14960

Open faizcode24 opened 1 week ago

faizcode24 commented 1 week ago

Prerequisites

Mongoose version

8.7.1

Node.js version

18.13.0

MongoDB server version

i was just using Atlas

Typescript version (if applicable)

No response

Description

mongoose new version having issues while connecting the MongoDB cluster, and having the error as

Error connecting in MongoDB Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist:

despite using the 0.0.0.0 ID address its still has the issues

import mongoose from "mongoose";
const dbConnect = async () => {
    try {

       await mongoose.connect(process.env.MONGODB_URI)

        console.log("Connected to MongoDB")
    }
    catch (error){
        console.log(`Database connection failed ${error.message}`);

    }

Steps to Reproduce

The issue was resolved after using the older stable version 8.1.1

Expected Behavior

No response

vkarpov15 commented 6 days ago

I'm unable to repro, we have several production projects running Mongoose 8.7 connected to Atlas with no issues.

Please provide:

  1. The MongoDB server version you're running on MongoDB Atlas
  2. The error.reason property from the MongooseServerSelectionError that has the Error connecting in MongoDB Could not connect to any servers in your MongoDB Atlas cluster message you pasted. Please make sure to omit any hostnames.
calvintny commented 5 days ago

Had the same issue doing tutorials with mongoose. Issue resolved after I rolled back to 8.6.3.

Mongoose version 8.7.1

Node.js version 18.16.0

MongoDB server version Atlas 7.0.14

error.reason :

TopologyDescription {
  type: 'ReplicaSetNoPrimary',
  servers: Map(3) {
    'XXX:27017' => ServerDescription {
      address: 'XXX:27017',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 5393564,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      primary: null,
      me: null,
      '$clusterTime': null
    },
    'XXX:27017' => ServerDescription {
      address: 'XXX:27017',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 5387737,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      primary: null,
      me: null,
      '$clusterTime': null
    },
    'XXX:27017' => ServerDescription {
      address: 'XXX:27017',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 5386680,
      lastWriteDate: 0,
      error: null,
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      primary: null,
      me: null,
      '$clusterTime': null
    }
  },
  stale: false,
  compatible: true,
  heartbeatFrequencyMS: 10000,
  localThresholdMS: 15,
  setName: 'XXX',
  maxElectionId: null,
  maxSetVersion: null,
  commonWireVersion: 0,
  logicalSessionTimeoutMinutes: null
}
vkarpov15 commented 5 days ago

I'm unable to repro. Running the following script against an Atlas cluster running MongoDB 7.0.14 with Mongoose 8.7.1:

const mongoose = require('mongoose');

mongoose.connect('mongodb+srv://user:pwd@abc.xyz.mongodb.net/?retryWrites=true&w=majority&appName=xyz').then(() => console.log('Connected', mongoose.connection.client.topology.s.description));

Outputs:

Connected TopologyDescription {
  type: 'ReplicaSetWithPrimary',
  servers: Map(3) {
    'abc.mongodb.net:27017' => ServerDescription {
      address: 'abc.mongodb.net:27017',
      type: 'RSSecondary',
      hosts: [Array],
      passives: [],
      arbiters: [],
      tags: [Object],
      minWireVersion: 0,
      maxWireVersion: 21,
      roundTripTime: 52,
      minRoundTripTime: 0,
      lastUpdateTime: 4511622,
      lastWriteDate: 2024-10-15T15:49:45.000Z,
      error: null,
      topologyVersion: [Object],
      setName: 'TTT',
      setVersion: 109,
      electionId: null,
      logicalSessionTimeoutMinutes: 30,
      primary: 'abc.mongodb.net:27017',
      me: 'abc.mongodb.net:27017',
      '$clusterTime': [Object]
    },
    '123.mongodb.net:27017' => ServerDescription {
      address: '123.mongodb.net:27017',
      type: 'RSPrimary',
      hosts: [Array],
      passives: [],
      arbiters: [],
      tags: [Object],
      minWireVersion: 0,
      maxWireVersion: 21,
      roundTripTime: 52,
      minRoundTripTime: 0,
      lastUpdateTime: 4511630,
      lastWriteDate: 2024-10-15T15:49:45.000Z,
      error: null,
      topologyVersion: [Object],
      setName: 'TTT',
      setVersion: 109,
      electionId: new ObjectId('7fffffff000000000000031f'),
      logicalSessionTimeoutMinutes: 30,
      primary: '123.mongodb.net:27017',
      me: '123.mongodb.net:27017',
      '$clusterTime': [Object]
    },
    'xyz.mongodb.net:27017' => ServerDescription {
      address: 'xyz.mongodb.net:27017',
      type: 'RSSecondary',
      hosts: [Array],
      passives: [],
      arbiters: [],
      tags: [Object],
      minWireVersion: 0,
      maxWireVersion: 21,
      roundTripTime: 49,
      minRoundTripTime: 0,
      lastUpdateTime: 4511636,
      lastWriteDate: 2024-10-15T15:49:45.000Z,
      error: null,
      topologyVersion: [Object],
      setName: 'TTT',
      setVersion: 109,
      electionId: null,
      logicalSessionTimeoutMinutes: 30,
      primary: 'xyz.mongodb.net:27017',
      me: 'xyz.xvnqv.mongodb.net:27017',
      '$clusterTime': [Object]
    }
  },
  stale: false,
  compatible: true,
  heartbeatFrequencyMS: 10000,
  localThresholdMS: 15,
  setName: 'TTT',
  maxElectionId: new ObjectId('7fffffff000000000000031f'),
  maxSetVersion: 109,
  commonWireVersion: 0,
  logicalSessionTimeoutMinutes: 30
}

@calvintny what OS are you using, maybe you're on Windows? I've tested on OSX and linux, both work fine.

calvintny commented 1 day ago

yes im using windows