0

I have to use Django with a legacy MongoDB database. I have made the connection but I don't know how to access the documents/data present inside the database?

For relational legacy databases, we use inspectdb command and we get references for the existing tables/models but how do we query existing data in legacy non-relational databases?

I am making a connection using mongoengine

myclient = connect('db_name',host='host_ip', port=post_number, username='db_user', password='db_password', authentication_source='db_name ')

and I am able to retrieve data using

mydb = myclient['db_name']

mycol = mydb['collection_name']

mycol.find_one()

but I want to retrieve data using Django models.

Community
  • 1
  • 1
  • in case you couldn't find any answer for mongoengine (as question is tagged under that), you can use [mongodb shell](https://docs.mongodb.com/manual/reference/mongo-shell/) commands. – Nalin Dobhal Dec 12 '19 at 07:35

1 Answers1

0

Suppose model is:-

class Quote(Document):
    quote = StringField()
    author = StringField()

then you can save data as below-

quote_name = {"quote":"Any Quote","author":"xyz"} 
db_object = Quote(**quote_name)
db_object.save()

You can retrieve data as below:-

Quote.objects(author="xyz")

or

Quote.objects(quote="Any Quote")