Last 2 exercises on Mongodb and Mongoose can't be solved

Tell us what’s happening:
I was unable to solve last two exercises i updated to the versions that are given in mongodb and mongoose tutorial and i still cant pass the test even the code seems fine.

Your code so far
require( ‘dotenv’ ).config();
const mongoose = require( “mongoose” );
const { Schema } = mongoose;

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

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

} );

let Person = mongoose.model( ‘Person’, personSchema );

const createAndSavePerson = ( done ) => {
let darko = new Person( { name: ‘Darko’, age: 20, favoriteFoods: [‘Banana’, ‘Apples’] } );
darko.save( function ( err, data ) {
{
if ( err ) return console.log( err );
done( null, data );
}
} );

};

var arrayOfPeople = [
{ name: “Frankie”, age: 74, favoriteFoods: [“Del Taco”] },
{ name: “Sol”, age: 76, favoriteFoods: [“roast chicken”] },
{ name: “Robert”, age: 78, favoriteFoods: [“wine”] }
];

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

const findPeopleByName = ( personName, done ) => {
Person.find( { name: personName }, ( err, peopleFound ) => {
if ( err ) return console.log( err );
done( null, peopleFound );
}
);
};

const findOneByFood = ( food, done ) => {
Person.findOne( { favoriteFoods: food }, ( err, peopleFound ) => {
if ( err ) return console.log( err );
done( null, peopleFound );
} );
};

const findPersonById = ( personId, done ) => {
Person.findById( { _id: personId }, ( err, peopleFound ) => {
if ( err ) return console.log( err );
done( null, peopleFound );
} );
};

const findEditThenSave = ( personId, done ) => {
const foodToAdd = “hamburger”;
Person.findById( personId, ( err, person ) => {

// Array.push() method to add "hamburger" to the list of the person's favoriteFoods
person.favoriteFoods.push( foodToAdd );

person.save( ( err, updatedPerson ) => {
  if ( err ) return console.log( err );
  done( null, updatedPerson );
} );

} );
};

const findAndUpdate = ( personName, done ) => {
const ageToSet = 20;
Person.findOneAndUpdate( { name: personName }, { age: ageToSet }, { new: true },
( err, updatedPersonAge ) => {
if ( err ) return console.log( err );
done( null, updatedPersonAge );
} );
};

const removeById = ( personId, done ) => {
Person.findByIdAndRemove( personId, ( err, updatedPerson ) => {
if ( err ) return console.log( err );
done( null, updatedPerson );
} );
};

const removeManyPeople = ( done ) => {
const nameToRemove = “Mary”;
Person.deleteMany( { name: nameToRemove }, ( err, response ) => {
if ( err ) return console.log( err );
done( null, response );
} );
};

const queryChain = ( done ) => {
const foodToSearch = “burrito”;
Person.find( { favoriteFoods: foodToSearch } )
.sort( { name: ‘asc’ } )
.limit( 2 )
.select( ‘-age’ )
.exec( function ( err, data ) {
if ( err ) return console.log( err );
done( null, data );

} );

};

Your browser information:

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

Challenge: Chain Search Query Helpers to Narrow Search Results

Link to the challenge:
https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results and https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/delete-many-documents-with-model-remove)

Can you post a link to your project code?

Also did you try searching the forum for the specific challenge or problem you are having before posting your question. There have been many posts about the 2nd to last challenge and I am almost positive your question problem with the challenge has been answered before.

I saw those solutions but they do not work for me i have the versions of mongodb and mongoose the same as in tutorial. I tried through heroku and also i have tried submitting localhost:3000 but the result is the same. This is the error in terminal :

Your app is listening on port 3000
OPTIONS
POST
(node:9228) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

I have tried instead of person.remove to use deleteMany and the result is the same.
also node --trace-deprecation does nothing

darkosel3/MongoDb (github.com)

Looking at your package.json file, I do not see the same versions as specified in the first challenge of this set of challenges. A Quick search on the forum for remove method mongoose showed [this forum thread] that contains the same advice I am giving you now. This advice should help you solve the 2nd to last challenge. Once you get that working let us know if you are still having trouble with the last challenge.

I updated to the version in the tutorial and this is the error.


And when i turn my mongo_uri into string it shows this

Can you push your latest changes to GitHub? Did you make the changes to package.json and install the correct versions of the packages?

1 Like

darkosel3/MongoDB2 (github.com)

Yes i havee tried but nothing works

I suggest delete the node_modules folder and running npm ci which should install what is in the package-lock.json file. I pulled down your project from GitHub locally and your code passes the second to last challenge (like I expected it would). I do not see the errors you are showing above. I do see the DeprecationWarning, but that is to be expected and does not affect passing the challenge tests.

As far as the last challenge, your code passes fine.