0

I have an application where I let users access other third party applications and fetch data from them and perform some visualizations.For the ease of users, I only ask them to send the credentials once through POST request and then store it in Mongodb so next time onwards they ask for data without having to pass the credentials again and again.Now I plan to avoid duplication in my Mongodb database.So even though the user sends their credential, I don't reinsert it rather I use the upsert option.

users.update(doc, doc, upsert=True)

This does the trick but now when the user doesn't send any credentials, mongodb creates an object id for the particular request and puts None to value of each of the fields

{'account_id': None, 'password': None, 'username': None}

I have checked the following resource for the problem but the suggestions are specific to mongodb and not pymongo.

Avoid insertion in mongoDB Without Data

How do I ensure that I do not insert data with null values into my database?

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70

1 Answers1

1

You could use a simple if to make sure the doc is not None.

if doc:
    users.update(doc, doc, upsert=True)

A database level solution is create a collection schema as pointed at resource that you linked at your question. The validators will guarantee correct insertions and upsertions.