node.js – Mongoose Schemas and inserting via python

node.js – Mongoose Schemas and inserting via python

It is impossible because python and nodejs are 2 different runtimes – separate isolated processes which dont have access to each other memories.

Mongoose is a nodejs ORM – a library that maps Javascript objects to Mongodb documents and handles queries to the database.

All mongoose hooks belong to javascript space. They are executed on javascript objects before Mongoose sends any request to mongo. 2 outcomes from there: no other process can mess up with these hooks, not even another nodejs, and once the query reaches mongodb its final, no more hooks, no more modifications.

One said a picture worth 100 words:
enter

Neither python nor mongo are aware about mongoose hooks. All queries to mongo are initiated on the client side – a script sends a request to modify state of the database or to query state of the database.

The only way to trigger a javascript code execution from an update on mongodb side is to use change streams

Change streams are not mongoose hooks but can be used to hook into the updates on mongo side. Its a bit more advanced use of the database. It comes with additional requirements for mongo set up, size of the oplog, availability of the changestream clients, error handling etc.
You can learn more about change streams here https://docs.mongodb.com/manual/changeStreams/ I would strongly recommend to seek professional advice to architect such set up to avoid frustration and unexpected behaviour.

Mongo itself does not support hooks as a feature, mongoose gives you out of the box hooks you can use as youve mentioned. So what can you do to make it work in python?

  1. Use an existing framework like pythons eve, eve gives you database hooks, much like mongoose does. Now eve is a REST api framework which from your description doesnt sound like what youre looking for. Unfortunately I do not know of any package thats a perfect fit to your needs (if you do find one it would be great if you share a link in your question).

  2. Build your own custom wrapper like this one. You can just built a custom wrapper class real quick and implement your own logic very easily.

node.js – Mongoose Schemas and inserting via python

Leave a Reply

Your email address will not be published.