MongoEngine, loading model deletes existing documents

Viewed 163

I'm confused about why loading a mongonengine model deletes that collection from my mongodb database.

I'm running mongoengine (0.10.6) in a Python2.7 application and have had to create a script to save data into the database.

My script to save data uses this code (a minimal example of what's going on, so it's only the 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 that process, I can query the database later in the program and retrieve my data.

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

#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 has an error to say the Nonetype has no title.

When I open the mongo shell, mongo, I can see the databases I've created but there's nothing inside. I can run at my terminal:

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

If I run the above commands before the second process, it returns the expected documents and count. If I run it after the second process, all documents are gone. I even tried making a script that merely loaded the model for the saved documents and that deletes the documents.

Why does loading the model delete all instances of the data in my database?

Here is the (shortened) contents of education_models:

import mongoengine as mongo

class EducationProgram(mongo.Document): 
    program_title = mongo.StringField(required=True, unique = True) 
    program_code = mongo.StringField() 
    ...
0 Answers
Related