Skip to content

Tag: mongoose

Using new collections for each user

I am building an Expense Tracking app using Node.js where all the Income and Costs should get saved in a database. My idea is to create a new collection for each registered user and save all their income/cost actions in that collection. The things i would like to consider before writing it are: how do i name …

Using async functions as conditions

Lets say i want to run an if statment where the condition is async function. This is the function, it uses mongoose findOne which is an async task. The problem is that the if statment gets called before the con can get executed which then does’nt set the condition. Answer You can do it this way : Return…

mongoose.connect() is undefined

I’m trying to connect to my MongoDB using Mongoose and it gives me the following error. I get this Error Answer You should change 2 things: Change { mongoose } with mongoose Remove useNewUrlParser option. New version of Mongoose does not accept it as option and it will throw an error.

Destructuring a mongoose document

Using mongoose in my project, I ran into a problem. I want to find all documents that have such a key and value pair role: USER. I can get a list of documents, but I cannot get the values of specific fields from it, no matter how I try. Here is my code: The request definitely gets the document, because