Something Extra: $addFields to Aid Analysis

Introduction to MongoDB in Python

Donny Winston

Instructor

A somber $project

docs = list(db.laureates.aggregate([
    {"$project": {"died": {"$dateFromString": {"dateString": "$died"}},
                  "born": {"$dateFromString": {"dateString": "$born"}}}}
]))
OperationFailure: Error parsing date string '0000-00-00';
                  11: The parsed date was invalid ''
docs = list(db.laureates.aggregate([
    {"$match": {"died": {"$gt": "1700"}, "born": {"$gt": "1700"}}},
    {"$project": {"died": {"$dateFromString": {"dateString": "$died"}},
                  "born": {"$dateFromString": {"dateString": "$born"}}}}
]))
OperationFailure: Error parsing date string '1898-00-00';
                  11: The parsed date was invalid ''
Introduction to MongoDB in Python

$split$ and $cond$-itionally correct (with $concat)

docs = list(db.laureates.aggregate([

{"$match": {"died": {"$gt": "1700"}, "born": {"$gt": "1700"}}}, {"$addFields": {"bornArray": {"$split": ["$born", "-"]}, "diedArray": {"$split": ["$died", "-"]}}},
{"$addFields": {"born": {"$cond": [ {"$in": ["00", "$bornArray"]}, {"$concat": [{"$arrayElemAt": ["$bornArray", 0]}, "-01-01"]}, "$born" ]}}},
{"$project": {"died": {"$dateFromString": {"dateString": "$died"}}, "born": {"$dateFromString": {"dateString": "$born"}}, "_id": 0}} ])) print(docs[0])
{'died': datetime.datetime(1923, 2, 10, 0, 0),
 'born': datetime.datetime(1845, 3, 27, 0, 0)}
Introduction to MongoDB in Python

A $bucket list

docs = list(db.laureates.aggregate([
    ...,
    {"$project": {"died": {"$dateFromString": {"dateString": "$died"}},
                  "born": {"$dateFromString": {"dateString": "$born"}}}},
    {"$project": {"years": {"$floor": {"$divide": [
        {"$subtract": ["$died", "$born"]},
        31557600000 # 1000 * 60 * 60 * 24 * 365.25
    ]}}}},
    {"$bucket": {"groupBy": "$years",
                 "boundaries": list(range(30, 120, 10))}}
]))
for doc in docs: print(doc)
{'_id': 30, 'count': 1}
{'_id': 40, 'count': 6}
{'_id': 50, 'count': 21}
{'_id': 60, 'count': 87}
{'_id': 70, 'count': 154}
{'_id': 80, 'count': 221}
{'_id': 90, 'count': 115}
{'_id': 100, 'count': 2}
Introduction to MongoDB in Python

Practice $addFields

Introduction to MongoDB in Python

Preparing Video For Download...