How to store aggregation pipeline in mongoDB?
<p>I am developing a rule engine using MongoDB and Python. I want to store Rules in MongoDB in a Rule database "myrulesdb" and want to use it to run aggregation pipeline on a different collection. Rule saved in MongoDB is</p>

<pre><code>{
"_id" : ObjectId("57f46e843166d426a20d5e08"),
"Rule" : "[{\"$match\":{\"Name.First\":\"Sunil\"}},{\"$limit\":5}]",
"Description" : "You live long life"
}
</code></pre>

<p>Python Code:</p>

<pre><code>import pymongo
from pymongo import MongoClient

client = MongoClient('localhost', 27017)

db = client['myDB']
coll = db['myColl']

ruledb = client['myrulesdb']
rulecoll = ruledb['myrules']

rule1 = rulecoll.find_one({}, {"Rule":1, "_id":0})

pipe = rule1['Rule']
print(pipe)

data = coll.aggregate(pipeline=pipe)
</code></pre>

<p>I am getting correct string when i print pipe.</p>

<pre><code>[{"$match":{"Name.First":"Sunil"}},{"$limit":5}]
</code></pre>

<p>But when i pass this to aggregate() function, it gives me following error:</p>

<pre><code>data = coll.aggregate(pipeline=pipe)
in aggregate raise TypeError("pipeline must be a list")
TypeError: pipeline must be a list
</code></pre>

<p>After retrieving pipeline from database, how can i pass it to aggregate() function?</p>

<p>I don't know why you are doing this but the culprit here is the value of the <code>Rule</code> field which is string. If you try a little debuging with the <code>print()</code> function your will see that <code>type(pipe)</code> yields <code>&lt;class 'str'&gt;</code></p>

<p>You can fix this by loading the value using <a href="https://docs.python.org/3/library/json.html#json.loads" rel="nofollow"><code>json.loads</code></a> like this:</p>

<pre><code>&gt;&gt;&gt; import json
&gt;&gt;&gt; r = {"Rule" : "[{\"$match\":{\"Name.First\":\"Sunil\"}},{\"$limit\":5}]"}
&gt;&gt;&gt; pipe = json.loads(r['Rule'])
&gt;&gt;&gt; pipe
[{'$match': {'Name.First': 'Sunil'}}, {'$limit': 5}]
&gt;&gt;&gt; type(pipe)
&lt;class 'list'&gt;
</code></pre>

python mongodb rule-engine