MongoDB and Mongoose - Create and Save a Record of a Model

Getting some auth error while trying to save the new Person, I’ve triple and quadruple checked my username and password, looked up on google, and tried to check my database stats on cloud.mongoDB for any helpful info but I can’t get past this.

Relevant code:

require('dotenv').config();
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI);

let personSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  age: Number,
  favoriteFoods: [String]
});

let Person = mongoose.model('Person', personSchema);

const createAndSavePerson = (done) => {
  let newPerson = new Person({
    name: "Spooky",
    age: 31,
    favoriteFoods: ["Tacos", "Curry", "Chicken"]
  });
  //newPerson.save().then(done);
  newPerson.save(done);
};

Error:

MongoError: (Unauthorized) not authorized on admin to execute command { insert: "people", documents: [[{favoriteFoods [Tacos Curry Chicken]} {_id ObjectID("663be3b1949f8f0a22f94920")} {name Spooky} {age 31} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, lsid: { id: {4 [191 176 195 226 177 23 79 22 190 235 221 229 17 1 108 236]} }, txnNumber: 1.000000, $clusterTime: { clusterTime: {1715200942 7}, signature: { hash: {0 [66 165 27 96 170 221 69 192 24 6 144 34 200 123 52 76 171 112 239 134]}, keyId: 7316365815371005952.000000 } }, $db: "admin" }
    at Connection.<anonymous> (/workspace/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/pool.js:453:61)
    at Connection.emit (node:events:518:28)
    at processMessage (/workspace/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connection.js:456:10)
    at TLSSocket.<anonymous> (/workspace/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connection.js:625:15)
    at TLSSocket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError'
}

Note - I removed my URI from the .env variable just prior to snapshotting on account of containing my password but it passes at the initial connection so I really think that I have that right.

Gitpod Snapshot:
https://gitpod.io#snapshot/e7b7c7a0-5445-4361-9140-17e86e77e930

Lesson:

Alright I got it, I accidentally deleted the second argument to mongoose.connect() - useNewUrlParser: true, useUnifiedTopology: true }