Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created June 14, 2017 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/5c1275b8a4f00bd76af2a7c5079f51d3 to your computer and use it in GitHub Desktop.
Save podhmo/5c1275b8a4f00bd76af2a7c5079f51d3 to your computer and use it in GitHub Desktop.
import bson
import pymongo
from collections import defaultdict
from dictknife import pp
"""
Group -* User -* Skill
"""
client = pymongo.MongoClient("mongodb://localhost:27017/")
client.drop_database("test")
db = client["test"]
groups = [
{
"_id": bson.ObjectId(),
"name": "A"
},
{
"_id": bson.ObjectId(),
"name": "B"
},
]
users = [
{
"_id": bson.ObjectId(),
"group_id": groups[0]["_id"],
"name": "foo"
},
{
"_id": bson.ObjectId(),
"group_id": groups[0]["_id"],
"name": "bar"
},
{
"_id": bson.ObjectId(),
"group_id": groups[0]["_id"],
"name": "baz"
},
{
"_id": bson.ObjectId(),
"group_id": groups[1]["_id"],
"name": "boo"
},
]
skills = [
{
"_id": bson.ObjectId(),
"user_id": users[0]["_id"],
"name": "X"
},
{
"_id": bson.ObjectId(),
"user_id": users[1]["_id"],
"name": "X"
},
{
"_id": bson.ObjectId(),
"user_id": users[2]["_id"],
"name": "X"
},
{
"_id": bson.ObjectId(),
"user_id": users[1]["_id"],
"name": "Y"
},
{
"_id": bson.ObjectId(),
"user_id": users[2]["_id"],
"name": "Y"
},
{
"_id": bson.ObjectId(),
"user_id": users[2]["_id"],
"name": "Z"
},
]
db.groups.insert_many(groups)
db.users.insert_many(users)
db.skills.insert_many(skills)
# join by hand
users = list(db.users.find({}))
skills = db.skills.find({"user_id": {"$in": list({u["_id"] for u in users})}})
skill_map = defaultdict(list)
for s in skills:
skill_map[s["user_id"]].append(s)
groups = db.groups.find({"_id": {"$in": list({u["group_id"] for u in users})}})
group_map = {s["_id"]: s for s in groups}
tris = []
for u in users:
tris.append(dict(user=u, group=group_map[u["group_id"]], skills=skill_map[u["_id"]]))
pp(tris)

setup

$ docker-compose pull
$ docker-compose up
version: "3.0"
services:
mongo:
image: mvertes/alpine-mongo
volumes:
- .data:/data/db
ports:
- "27017:27017"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment