Can I use MongoDB schema model for defining IndexedDB indexes? - node.js

I am creating a progressive web app that that is using NodeJS and Express as backend, MongoDB as server, and IndexedDB for storing data locally when offline.
Currently I have defined some Mongoose schema models, and my application is suppose to fetch the data from my MongoDB server and store it into my local IndexedDB when the application goes online. Is it possible to make my IndexedDB's indexes follow the format of my Mongoose schema models, so that if I made some changes to the models, the IndexedDB's indexes will follow the changes as well.

The question is not so clear, an example would have help. But from what I understand, you want to be able to change the schema in MongoDB without breaking the documents saved in IndexedDB and you don't want to update the schema in IndexedDB each time you change the schema in MongoDB. You could use PouchDB which can use IndexedDB behind the scene and it would you to match the same schema as in your MongoDB. PouchDB integrates very well with MongoDB. Thus, if you model changes in MongoDB, when the document is eventually saved on PouchDB (IndexedDB), the document would have the same schema! For your info, PouchDB is the equivalent of MongoDB but in a browser.

Related

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?
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

Referring manually created collection stored in mongo atlas from node js

i am currently creating a nodejs app that deals with huge data. I created a table(.csv file) in excel and then converted it to to JSON file and added the document to MongoDB Atlas under the collection
players. The collection is huge, hence i did not create the collection inside from Node.JS. Now I want to refer to the fields inside the MongoDB collection from my Node.JS application and using it as a model..
how to solve this problem.....
You can just create a mongoose schema and model as you do normally. But, name the model player so that it points to your mongodb's players collection.

Connect multiple application with one mongo database using Mongoose (ORM)

I am using mongo database with mongoose (ORM). I have a database that is working well with Express framework Application. Now I want to connect to this database with another Express Application.
Now here is the question,
should I make all mongoose models again in new project with Mongoose ? if not then what will be best option for this that we can use still use existing database mongoose.
I am assuming you are not serving the data via a REST API then. If you create a REST API for the mongoose models and then serve that data through URL's you won't have to create new models, rather you could just request the data via HTTP through GET, POST, DELETE, etc requests from the new express application.
If you want me to elaborate further, I can certainty do so.

Npm modules of validation in mongodb

Is there a module for performing validation in core mongodb driver of node.js. Similary how mongoose does it. As I would require mongodb in a new client project instead of monoose.
MongoDB introduced validation in collections in version 3.2 -
MongoDB provides the capability to validate documents during updates and insertions. Validation rules are specified on a per-collection basis using the validator option, which takes a document that specifies the validation rules or expressions.
You can read more about it in the official docs of MongoDB here.
As far as Node.js MongoDB driver is concerned, then it will also provide this feature out of the box. No need to install any other module. You can check this official tutorial which shows you how to use the Node.js driver to set up validation.
Keep in mind one thing that Mongoose provides you with application level validation so you save on the round trip time as you don't need to go to the Mongo database to validate your data. Whereas the native validation is provided by the Mongo Database itself hence a round trip would be necessary from your application to your Mongo Database to validate the data.

Mongo Express doesn't correctly display embedded documents

I'm using Mongo Express to view my data in my MongoDB instance during development. I also use the connect-mongo module to store my sessions in MongoDB. For some reason, Mongo Express has difficulties parsing embedded documents created by connect-mongo. The indention doesn't quite work and quotes are escaped. Documents created in connect-mongo do work, as well as those created with mongoose.
Looks like connect-mongo stores its data as strings, rather than as embedded documents. You can try using the default MongoDB shell - mongo and confirm if it's the case.

Resources