MongoEngine loading model deletes existing documents

I am confused about why loading the mongonengine model removes this collection from my mongodb database.

I run mongoengine (0.10.6) in a Python2.7 application and had to create a script to save the data in the database.

My script uses this code to save data (a minimal example of what is happening, so this is only relevant information):

#add_data.py
import mongoengine as mongo
from models import education_models

db_name = 'db_test'
db_url = 'mongodb://localhost:27017'
mongo.connect(db_name, host=db_url)

education_models.EducationProgram(title="Program 1").save()

Using this process, I can query the database later in the program and get my data.

However, I have a separate process that does the same thing:

#get_data.py
import mongoengine as mongo
from models import education_models

db_name = 'db_test'
db_url = 'mongodb://localhost:27017'
mongo.connect(db_name, host=db_url)

record = education_models.EducationProgram.objects(title="Program 1").first()
print record.title

This means that Nonetype has no title.

When I open the mongo shell mongo, I see the databases that I created, but there is nothing inside. I can run my terminal:

show dbs
use db_test
db_test.education_program.findOne({title:"Program 1"})
db_test.education_program.count()

, . , . script, .

?

() education_models:

import mongoengine as mongo

class EducationProgram(mongo.Document): 
    program_title = mongo.StringField(required=True, unique = True) 
    program_code = mongo.StringField() 
    ...
+4

Source: https://habr.com/ru/post/1678146/


All Articles