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

Hello fellow campers,

I would like to ask for your help with this challenge, I feel that my code is fine, I did everything according to the documentation, but nevertheless it does not pass the test, this is my code…

require('dotenv').config();
/*Metodo para llamar la Variable de entorno*/
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);

/*let validator = require('validator')*/
/*Mongoose Schema*/
let personSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true
  },
  age: {
    type: Number,
    required: false
  },
  favoriteFoods: {
    type: [String], 
    required: true
  }/*,
  email:{
    type: String,
    required: true,
    unique: true,
    lowercase: true,
    validate: (value) => {
      return validator.isEmail(value) /*Función que valida que sea un formato correo hay que crear la variable al inicio 'validator'
    }
  }*/
});
let Person = mongoose.model('Person', personSchema);
/*Nuevo registro del Schema*/
var createAndSavePerson = (done) => {
  let personTest = new Person({
    name: 'Person Test',
    age: 25,
    favoriteFoods: ['Hamburger', 'Hot Dog', 'Ice cream']/*,
    email: 'persontest@mail.com'*/
  });
  personTest.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data)
  });
};
/*Crear varios registros*/
  /* Primero se crea una variable con una matriz de objetos, segun el Schema*/
let arrayOfPeople = [
{name: "Frank Rocha", age: 74, favoriteFoods: ["Del Taco"]/*, email: "email1@mail.com"*/},
{name: "Sol Zolano", age: 76, favoriteFoods: ["roast chicken"]/*, email: "email2@mail.com"*/},
{name: "Robert Deniro", age: 78, favoriteFoods: ["wine"]/*, email: "email3@mail.com"*/}
];
/* Mediante esta función se hace el llamado del array-string creado anteriormente */
var createManyPeople = (arrayOfPeople, done) => {
  Person.create(arrayOfPeople, (err, people) => {
    if (err) return console.error(err);
    done(null, people);
  });
};


And this is the response from the console…

 npm run start

> fcc-mongo-mongoose-challenges@0.0.1 start
> node server.js

(node:146) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
Your app is listening on port 3000
(node:146) [MONGODB DRIVER] Warning: Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.
(node:146) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
OPTIONS
POST
(node:146) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
MongoError: E11000 duplicate key error collection: fcc-mongodb-and-mongoose.people index: email_1 dup key: { email: null }
    at Function.create (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/error.js:59:12)
    at toError (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/utils.js:130:22)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/operations/common_functions.js:258:39
    at handler (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/topologies/replset.js:1211:22)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/connection/pool.js:407:18
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  driver: true,
  index: 0,
  code: 11000,
  keyPattern: { email: 1 },
  keyValue: { email: null }
}

If anyone has any idea what might be going on, I’d be very grateful.

I’m working on repeat

I think you should give the past challenges a check. When connecting to the database using Mongoose, mongoose.connect(process.env.MONGO_URI), you should pass a second argument with the following object: { useNewUrlParser: true, useUnifiedTopology: true }.

Full statement:
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }).

1 Like

Thanks for your valuable contribution friend, I had not noticed that missing item, but additionally that error was appearing because in mongoose atlas, a collection had already been created with the name that I was registering, so I had to delete it from Atlas and return to pass the test and there if I pass,

Thank you for supporting those of us who are learning…

1 Like

No problem
I’m also a learner too. So good luck with your future projects and freeCodeCamp challenges :smiley: