Search code examples
node.jsmongodbmongoosemongoose-schema

How mongoose schema relates to db in CRUD operations


How does fetching records from mongoose find() and updating records with updateOne work exactly.

I am unable to find it on social media.. When a GET request arrived to route handler. Will data fetch from db and maps to schema or will it get records directly from db?

Please explain the proper workflow. What happen when request arrives and how the mapping from db to schema works in CRUD?


Solution

  • Making the assumption that you are also using a framework like expressjs, a GET request does not go directly to mongodb

    within a framework like express, you write a handler to handle GET/POST/PATCH/DELETE/PUT etc. within each of those routes you would write your code to interact with mongoose (mongodb)

    for reference here is a basic entry level NODEJS app