Search code examples
node.jsmongodbmongoose

atlas .find returning empty array while local mongodb works fine


no errors console logs []

const mongoose = require('mongoose'); 

const uri = "mongodb+srv://:@cluster0.yhy4bwu.mongodb.net/?retryWrites=true&w=majority";

const playerSchema = new mongoose.Schema({
    fullname: String,
    goals: Number,
    assists: Number, 
    gamesPlayed: String,
    shots: Number, 
    positionCode: String, 
    plusminus: String, 
    team: String, 
    timeOnIcePerGame: Number, 
    espnId: Number
})


mongoose.connect(uri)
    .then(async ()=>{
        model = mongoose.model('PlayersV3', playerSchema, 'PlayersV3')

        const doc = await model.find({fullname : {$regex :  new RegExp('a', "i")}}).limit(15).sort({goals: -1});
        console.log(doc)
    }).catch((error)=>{
        console.log(error)
    });

const local_uri = "mongodb://127.0.0.1:27017/nhl" the localhost uri works

I have made sure that I have not misspelled my PlayersV3 collection

anything that would point me in the right direction helps


Solution

  • try using this one uri assuming that your db name is "nhl" and also add you atlas username and password in uri.

    syntax:

    "mongodb+srv://<atlas username>:<password>@cluster0.yhy4bwu.mongodb.net/<database name>?retryWrites=true&w=majority";
    

    example:

     const uri = "mongodb+srv://<atlas username>:<password>@cluster0.yhy4bwu.mongodb.net/nhl?retryWrites=true&w=majority";