MongoDB and Mongoose - Create Many Records with model.create()

hello can anyone could help me ,when I run my code it doesn’t work I have the impression that my mongoose no longer wants to connect yet I’ve been connected since

this are my code
mongoose.connect(process.env['MONGO_URI'], { useNewUrlParser: true, useUnifiedTopology: true });



/** 2) Create a 'Person' Model */
var personSchema = new mongoose.Schema({
  name: String,
  age: Number,
  favoriteFoods: [String]
});

/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);

var createAndSavePerson = function(done) {
  var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["eggs", "fish", "fresh fruit"]});

  janeFonda.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data)
  });
};

/** 4) Create many People with `Model.create()` */
var arrayOfPeople = [
  {name: "Frankie", age: 15, favoriteFoods: ["Del Taco"]},
  {name: "Sol", age: 12, favoriteFoods: ["roast chicken"]},
  {name: "Robert", age: 34, favoriteFoods: ["wine"]}
];

var createManyPeople = function(arrayOfPeople, done) {
  Person.create(arrayOfPeople, function (err, people) {
    if (err) return console.log(err);
    done(null, people);
  });
};

this is console

> node server.js

Your app is listening on port 3000
OPTIONS
POST
/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:847
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: 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: https://docs.atlas.mongodb.com/security-whitelist/
    at Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:847:32)
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:351:10
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:1149:10)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:350:20)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/myApp.js:3:10)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/server.js:67:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(3) {
      'ac-2lvmfyc-shard-00-00.guig8mn.mongodb.net:27017' => ServerDescription {
        address: 'ac-2lvmfyc-shard-00-00.guig8mn.mongodb.net:27017',
        error: MongoNetworkError: connection <monitor> to 34.199.61.99:27017 closed
            at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
            at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
            at TLSSocket.emit (node:events:513:28)
            at node:net:313:12
            at TCP.done (node:_tls_wrap:587:7),
        roundTripTime: -1,
        lastUpdateTime: 70556777,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      },
      'ac-2lvmfyc-shard-00-01.guig8mn.mongodb.net:27017' => ServerDescription {
        address: 'ac-2lvmfyc-shard-00-01.guig8mn.mongodb.net:27017',
        error: MongoNetworkError: connection <monitor> to 18.207.47.50:27017 closed
            at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
            at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
            at TLSSocket.emit (node:events:513:28)
            at node:net:313:12
            at TCP.done (node:_tls_wrap:587:7),
        roundTripTime: -1,
        lastUpdateTime: 70556066,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      },
      'ac-2lvmfyc-shard-00-02.guig8mn.mongodb.net:27017' => ServerDescription {
        address: 'ac-2lvmfyc-shard-00-02.guig8mn.mongodb.net:27017',
        error: MongoNetworkError: connection <monitor> to 18.207.36.39:27017 closed
            at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
            at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
            at TLSSocket.emit (node:events:513:28)
            at node:net:313:12
            at TCP.done (node:_tls_wrap:587:7),
        roundTripTime: -1,
        lastUpdateTime: 70557556,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      }
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

Node.js v18.12.1

the link :
https://replit.com/@bowak/boilerplate-mongomongoose)
chalange link:
https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/create-many-records-with-model-create

Reset your password in MongoDB Atlass (Website) > Security > Database Access

The same credentials need to be exact in the connection string Replit’s secrets. And the connection string should have no quotes around it for Replit use.

thank you but i have change my password but is not working always

I was just looking at the article which @camperextraordinaire mentioned, as I spotted that too.

Here’s a link: MongoDB Atlas Tutorial – How to Get Started
Specifically, this section:

Allow Access From All IP Addresses

  • On the left side of the screen under SECURITY, click on Network Access.
  • Click the green Add IP Address button.
  • In the modal, click the ALLOW ACCESS FROM ANYWHERE button. You should see 0.0.0.0/0 in the Access List Entry field.
  • Click the green Confirm button.

If you log in to MongoDB Atlas, you can reconfigure access to your clusters via the Network Access menu on the left of the screen.

1 Like

man you’re amazing i was looking for a solution for this problem for many hours and you helped me a lot
thanks

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.