Unable to connect to MongoDB server

const mongodb = require('mongodb').MongoClient;
const mongoose = require('mongoose');

const puppySchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  age: Number

const Puppy = mongoose.model('Puppy', puppySchema);
const kittySchema = new mongoose.Schema({
  name: String
kittySchema.methods.speak = function speak() {
  const greeting = this.name
    ? 'Meow name is ' + this.name
    : 'I don\'t have a name';
const Kitten = mongoose.model('Kitten', kittySchema);
const fluffy = new Kitten({ name: 'fluffy' });

// Database Connection

const uri = "mongodb+srv://user:password@cluster0.9qmmvce.mongodb.net/?retryWrites=true&w=majority";
const client = new mongodb(uri, { 
    useNewUrlParser: true,
    useUnifiedTopology: true 
console.log('Connected to Atlas');
client.connect(err => {
  const collection = client.db("test2").collection("devices");
  // perform actions on the collection object
    then((doc) => {
  .catch((err) => {
    const kittens = Kitten.find()
    .then((doc) => {
  .catch((err) => {

It prints ‘Connected to Atlas’ but nothing happens.

EDIT: I tried on several different machines and still the same. Tried locally and Atlasdb.

  • what do you expect this code to do?
  • which means it is connected to db which is good, now question is what do you want this to do more than that? you are not making any “CRUD” operations from where i can see, unless calling “speak()” will invoke any “CRUD” functionality from behind scenes, which is unlikely…

I tried all sorts of different code examples but always had errors so gave up and moved to SQL. Thanks.

This is not a valid connection string.

  1. It should contain your actual username and password.
  2. It SHOULD NOT be in your public code but in an environment variable, tucked away out of sight.

The connection log should be inside the connect callback. Or after an await or inside a .then(). The log you have now will always print (unless the code before it throws). You can send a ping command as well. Look at the docs.

Callbacks are kind of dead, use the promise/async way instead.

As you are using Mongoose you should stick to that. Don’t mix the native driver API and Mongoose. You already have code from the Mongoose docs so I’m not sure why you are connecting like you are and mixing things.

Just because “nothing happens” doesn’t mean you are not connecting to the DB.

Connecting to the DB and performing an action on a collection/model are two different things. I’d suggest you read the docs of the libs you are using and follow the tutorials they have.


1 Like

I’m a beginner in MEAN stack development so, not have much idea. But I found this helpful link, you can check it here:

Also you can contact us: