I am using mongoDB as my database, i am working on django projects and using mongoengine to connect with database. My question is if my default database in settings.py
is DB1
and i want to delete all records of a collection which exists inside DB2
then how can i do this.
settings.py
import mongoengine
mongoengine.connect(
db= "DB1",
host='localhost',
)
models.py
class Resources(Document):
field1= fields.StringField(max_length=20)
field2 = fields.StringField(max_length=70)
field3 = fields.StringField(max_length=70)
field4 = fields.DictField()
meta = {'collection': 'resources', 'allow_inheritance': False, '_target_db': 'DB2'}
python shell
from .models import Resources
import mongoengine
mongoengine.connect('DB2', alias='ces')
ob = Resources()
ob.switch_db('ces')
ob.field1 = value
ob.field2 = value
ob.field3 = value
ob.save()
Now i have collection resources
in DB2
and it has some records, i have tried Resources.objects.all().delete()
but it is not deleting records form DB2
instead it deletes records from DB1
which is default database.