Skip to content

Instantly share code, notes, and snippets.

@knoguchi
Created December 4, 2018 06:16
Show Gist options
  • Save knoguchi/0c2ffba6c2e467961c514b0a1fedf32e to your computer and use it in GitHub Desktop.
Save knoguchi/0c2ffba6c2e467961c514b0a1fedf32e to your computer and use it in GitHub Desktop.
Food for thought: can we access MongoDB like LINQ?
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