Created
December 4, 2018 06:16
-
-
Save knoguchi/0c2ffba6c2e467961c514b0a1fedf32e to your computer and use it in GitHub Desktop.
Food for thought: can we access MongoDB like LINQ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functional import seq | |
from pymongo import MongoClient | |
class dotdict(dict): | |
"""dot.notation access to dictionary attributes""" | |
__getattr__ = dict.get | |
__setattr__ = dict.__setitem__ | |
__delattr__ = dict.__delitem__ | |
client = MongoClient('mongodb://localhost:27017/') | |
db = client.sample | |
collection = db.tweets | |
s = seq(collection.find()).map(lambda e: dotdict(e)) | |
# I want to access Mongo like this. | |
print(s | |
.where(lambda e: len(e.text) < 3) | |
.size() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment