Using PyMongo insert_many and empty/null values
<p>I'm in the process of populating a mongoDB database and and not sure what to do with null values when using the insert_many statement (I should state at this point that I'm new to both Python and MongoDB)</p>

<p>The data I am inserting is two dimensional traditional SQL like data obtained from a text file, it looks something like this:</p>

<pre><code>emp  = [
         [7839, 'KING', 'PRESIDENT', null], 
         [7698, 'BLAKE', 'MANAGER', 7839],
         [7782, 'CLARK', 'MANAGER', 7839],
         [7566, 'JONES', 'MANAGER', 7839],
         [7788, 'SCOTT', 'ANALYST', 7566],
         [7902, 'FORD', 'ANALYST', 7566],
         [7369, 'SMITH', 'CLERK', 7902], 
         [7499, 'ALLEN', 'SALESMAN', 7698],
         [7521, 'WARD', 'SALESMAN', 7698],
         [7654, 'MARTIN', 'SALESMAN', 7698],
         [7844, 'TURNER', 'SALESMAN', 7698],
         [7876, 'ADAMS', 'CLERK', 7788],
         [7900, 'JAMES', 'CLERK', 7698],
         [7934, 'MILLER', 'CLERK', 778]
       ]
</code></pre>

<p>And my database population looks like this</p>

<pre><code>employee.insert_many([{
                       "_id" : emp[i,0],
                       "Name": emp[i,1],
                       "Role": emp[i,2],
                       "Boss": emp[i,3]
                      }
                      for i in range(len(emp))
                      ],False)
</code></pre>

<p>Ideally I would like "KING", the president, to not have the "Boss" field but I'm not sure how to achieve this. Could anyone point me in the right direction?</p>

<p>When you have a datafile like containing:</p>

<pre><code>1,'Bob','Owner',null
2,'Joe','Worker','Bob'
3,'Sergiu','Eng','Bob'
</code></pre>

<p>insert_many it accepts an array (an iterable), in youre code you are using it wrong; python code can look like this:</p>

<pre><code># 1 - create a list, read data and insert (computed) documents
documents = []
with open("data.txt", "r") as data:
    for line in data:
        line = line.strip('\n')
        #Compute your documents
        doc_array = line.split(',')
        doc = {}
        doc['_id'] = int(doc_array[0])
        doc['name'] = doc_array[1].strip('\'')
        doc['role'] = doc_array[2].strip('\'')
        boss = doc_array[3].strip('\'')
        if boss != 'null':
            doc['boss'] = boss
        documents.append(doc)
#2 use the list for insert_many function
db.test.insert_many(documents)
</code></pre>

python mongodb pymongo bulkinsert