#StackBounty: #python #mongodb #aggregate #pymongo #updates MongoDB collection update with $set and aggregate

Bounty: 50

I need to change the type of timestamp_ms filed from string to double and create FixedDate field which is based on this new timestamp_ms field. Than, change timestamp info into ISO date in NewDate field.

I used this code:

collection.update({
    "FixedDate": {"$exists": False}
},[{
    "$set":
        {"FixedDate":
            {"$convert":
                {"input": "$timestamp_ms",
                "to": "double"
                }
            }
        }
    },
    {"$set":
        {"NewDate":
                {"$toDate": "$FixedDate"
                }
            }
    }
], multi=True)

It gives this error message:

TypeError: document must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping

My data looks like this:

{'_id': ObjectId('5afea920d326051990a7f337'), 'created_at': 'Fri May 18 10:21:07 +0000 2018', 'timestamp_ms': '1526638867739'}
{'_id': ObjectId('5afea920d326051990a7f339'), 'created_at': 'Fri May 18 10:21:08 +0000 2018', 'timestamp_ms': '1526638868310'}
{'_id': ObjectId('5afea972d326051c5c05bc11'), 'created_at': 'Fri May 18 10:22:30 +0000 2018', 'timestamp_ms': '1526638950799'}
{'_id': ObjectId('5afea974d326051c5c05bc16'), 'created_at': 'Fri May 18 10:22:32 +0000 2018', 'timestamp_ms': '1526638952160'}
{'_id': ObjectId('5afea974d326051c5c05bc17'), 'created_at': 'Fri May 18 10:22:32 +0000 2018', 'timestamp_ms': '1526638952841'}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.