join_example.py 1.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from pymongo import MongoClient
import argparse
from pprint import pprint

parser = argparse.ArgumentParser(description = "Perform a join between the experiment and sample collections.")
parser.add_argument("--password", dest="password", required=True, help="Database user password.")
parser.add_argument("--username", dest="username", default="aknecht", required=True, help="Database user.")
parser.add_argument("--host", dest="host", default="hcc-anvil-241-41.unl.edu", required=True, help="Database host.")
args = parser.parse_args()

# Everything in a db named chipseq
# Set up connection and authenticate

client = MongoClient(args.host)
db = client.chipseq
db.authenticate(args.username, args.password, mechanism="SCRAM-SHA-1")

cursor = db.experiments.aggregate([
    {
        "$lookup": {
            "from": "samples",
            "localField": "_id",
            "foreignField": "experiment_id",
            "as": "samples"
        }
    }
    ## Potentially more aggregation steps here.
])

for document in cursor:
    if len(document["samples"]) > 0:
        pprint(document)
        break