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

Tell us what’s happening:

I successfully complete the challenge, then I tried to check the collection in MongoDB (cloud.mongodb.com), but cannot find the collection that I just created. Is there something wrong with my code?

Your code so far

require('dotenv').config();
var mongoose = require('mongoose');


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

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

const Person = mongoose.model("Person", personSchema);

const createAndSavePerson = (done) => {
  var nadhira = new Person({name: "Nadhira F",age: 16,favoriteFoods: ["mie", "ayam", "sop"]})
  nadhira.save(function(err,data){
    if(err) return console.error(err);
    done(null , data);
  })
  
};

var arrayOfPeople = [
  {name: "Adam", age: 12, favoriteFoods: ["Ayam", "Kentang", "Nugget"]},
  {name: "Jumbo", age: 9, favoriteFoods: ["Burger", "Pizzza"]}
]; 

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

const findPeopleByName = (personName, done) => {
  done(null /*, data*/);
};

const findOneByFood = (food, done) => {
  done(null /*, data*/);
};

const findPersonById = (personId, done) => {
  done(null /*, data*/);
};

const findEditThenSave = (personId, done) => {
  const foodToAdd = "hamburger";

  done(null /*, data*/);
};

const findAndUpdate = (personName, done) => {
  const ageToSet = 20;

  done(null /*, data*/);
};

const removeById = (personId, done) => {
  done(null /*, data*/);
};

const removeManyPeople = (done) => {
  const nameToRemove = "Mary";

  done(null /*, data*/);
};

const queryChain = (done) => {
  const foodToSearch = "burrito";

  done(null /*, data*/);
};






/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
 */

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0

Challenge Information:

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

Do you have more information, what are you using to run the project. I don’t know if this could be it but you might want to check this. ["Burger", "Pizzza"]}

I believe some of the test will delete the data it creates after it is done. So if you have only run the code using the challenge submissions, it may not be in the DB. If you look in the server.js file, you can see the test routes and code.

But if you manually run the code and create the data, it should be in your collection.

I run the code locally using VS Code, so it is hosted to http://localhost:3000

So if I run the code in VS Code locally, I run it using “npm run start” command, it won’t post the data in my MongoDB collection?

No, that isn’t what I’m saying. You have functions that are only called by the tests, and (at least some) of the tests does some post testing clean up. If you call the functions yourself, they should create data in your collections that remain.

I tried call the function myself and it works! Thanks