Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Schemas and inserting via python

I'm using a Nodejs server for a WebApp and Mongoose is acting as the ORM.

I've got some hooks that fire when data is inserted into a certain collection.

I want those hooks to fire when a python script inserts into the mongoDB instance. So if I have a pre save hook, it would modify the python scripts insert according to that hook.

Is this possible? If so, How do I do it?

If not, please feel free to explain to me why this is impossible and/or why I'm stupid.

EDIT: I came back to this question some months later and cringed just at how green I was when I asked it. All I really needed done was to create an API endpoint/flag on the NodeJS server that is specifically for automated tasks like the python script to send data to, and have mongoose in NodeJS land structure.

like image 955
Abe Avatar asked Apr 21 '20 03:04

Abe


People also ask

Can we use Mongoose with Python?

It is impossible because python and nodejs are 2 different runtimes - separate isolated processes which don't 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.

How do I transfer data from Python to MongoDB?

Insert Into Collection To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method. The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert.

What is the difference between schema and model in Mongoose?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.


2 Answers

It is impossible because python and nodejs are 2 different runtimes - separate isolated processes which don't 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 it's final, no more hooks, no more modifications.

One said a picture worth 100 words: enter image description here

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. It's 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.

like image 85
Alex Blex Avatar answered Nov 14 '22 23:11

Alex Blex


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

  1. Use an existing framework like python's eve, eve gives you database hooks, much like mongoose does. Now eve is a REST api framework which from your description doesn't sound like what you're looking for. Unfortunately I do not know of any package that's 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.

like image 41
Tom Slabbaert Avatar answered Nov 15 '22 00:11

Tom Slabbaert