prout
prout

Reputation: 367

Unable to get individual mongo document

I am fairly new to python and mongodb and I'm facing an issue already.

I am trying to "translate" a nodejs backend restapi into flask, using mongodb as a data source.

Using the flask documentation, I was able to configure my app in order to connect to my local mongod.

And I am able to obtain values from the users collection like this

def getUser():
    usr = Users.objects(email="[email protected]")
    return {
        "user": usr,
    }

Which returns the following JSON when I'm calling the API through Postman

{
    "user": [
        {
            "__v": 0,
            "_id": {
                "$oid": "5da86dc651eac87d2a82e2e2"
            },
            "createdAt": {
                "$date": 1571319238918
            },
            "email": "[email protected]",
            "password": "$2b$10$hoH57R5GL1MrwqpuW4yEJ.wwLlyNgyfxQm2Mxb19wioYTPPsU9z7y",
            "profil": {
                "_id": {
                    "$oid": "5da86dc651eac87d2a82e2e3"
                },
                "checked": false,
                "clapList": [],
                "followerList": [],
                "followingList": [],
                "playpoint": 0
            },
            "updatedAt": {
                "$date": 1571319477959
            }
        }
    ]
}

As you can see, I have an array with one user in it. When I try to get only one object, like this:

def getUser():
    usr = Users.objects(email="[email protected]").first()
    return {
        "user": usr,
    }

I have a 500 status returned in Postman and the following error in my debug console: mongoengine.errors.FieldDoesNotExist: The fields "{'__v'}" do not exist on the document "Users"

This is my Users model

import mongoengine as me

class Users(me.Document):
    phone = me.StringField()
    email = me.StringField()
    password = me.StringField()
    accountType = me.StringField()
    createdAt = me.DateTimeField()
    updatedAt = me.DateTimeField()
    profil = me.EmbeddedDocumentField(Profil)

I have already tried adding __v as an InfField(), but I still have the same error.

What is that __v anyway and should I retry making a new database from scratch?

Additional info:

Upvotes: 1

Views: 524

Answers (1)

prout
prout

Reputation: 367

So I added a meta property to it and I'm now able to use the class

meta = {
    'strict': False,
}

I don't really know yet what transpired in there but I'm not touching anything if it works

Upvotes: 1

Related Questions