-
Notifications
You must be signed in to change notification settings - Fork 1
/
srKblink-insert.py
40 lines (38 loc) · 1.19 KB
/
srKblink-insert.py
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
34
35
36
37
38
39
40
__author__ = 'asifj'
import requests
from pymongo import MongoClient
import json
import csv
import traceback
import logging
from tabulate import tabulate
from bson.json_util import dumps
client = MongoClient('10.219.48.134', 27017)
#client = MongoClient('192.168.56.101', 27017)
db = client['ImportedEvents']
collection = db['srKbLink']
collection_new = db['srKbLink-new']
document_no = 0
documents = collection.find(no_cursor_timeout=True)
inserts = 0
updates = 0
for document in documents:
for key, value in document.iteritems():
document[key] = str(value).strip()
key = {'caseId': document['SRID']}
doc = collection_new.find_one({'caseId': document['SRID']})
del document['_id']
if not doc:
inserts += 1
doc_to_ins = key
doc_to_ins['link'] = []
for key, value in document.iteritems():
document[key] = str(value).strip()
doc_to_ins['link'].append(document)
collection_new.insert(doc_to_ins)
else:
updates += 1
doc['link'].append(document)
collection_new.update(key, doc, upsert=True);
print "Inserts: "+str(inserts)
print "Updates: "+str(updates)