2
votes

This is my first time using pymongo. I have a method that updates Users data in a document. For example, when you log in, it should update the lastLog field, which indicates the last time that user logged in.

This is the method

    def update_user(self, mongo, field, value=None):
    print self.username
    if field != 'lastLog':
        result = mongo.db.Users.update(
                    {'username': self.username},
                    {
                        '$set': {
                            field: value
                        }
                    }
                )
    else:
        result = mongo.db.Users.update(
                    {'username': self.username},
                    {
                        '$set': {
                            '$currentDate': {
                                'LastLog': {
                                    "$type": "timestamp"
                                }
                            }
                        }
                    }
                )
    if result.matched_count != 1:
        #NEEDLOG
        print "No update performed"
        return False

However, everytime I log in, I get this error:

WriteError: The dollar ($) prefixed field '$currentDate' in   '$currentDate' is not valid for storage.

This is how the document looks in MongoDB

db.Users.find() { "_id" : ObjectId("57b64e1330e6e23b7d050c76"), "username" : "arecalde-contractor", "lastLog" : null, "Name" : "Agustin Recalde", "url" : "/profile/arecalde-contractor", "role" : "Admin", "active" : true, "id" : "9249" }

I'm pretty sure I'm following the documentation correctly. Did I miss anything? Thanks in advance!

1
I fixed it. I was doing it wrong. The code in the else now looks like this. - Agustin

1 Answers

1
votes

I fixed it. I was doing it wrong. Te code inside the else now looks like this

    else:
        result = mongo.db.Users.update_one(
                    {'username': self.username},
                    {
                      '$currentDate': {
                        'lastLog': {'$type': 'timestamp'}
                      }
                    }
                )