Last active
August 29, 2015 14:05
-
-
Save jlcampana/0353cbf3b8fefd744dd3 to your computer and use it in GitHub Desktop.
Mongo: check if exist element (optimized)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #estamos guardando de un CSV y no tenemos PK | |
| def get_db(): | |
| from pymongo import MongoClient | |
| client = MongoClient('localhost:27017') | |
| db = client.dineros | |
| return db | |
| def storeData(data): | |
| db = get_db() | |
| insertData = [] | |
| for d in data: | |
| #Chequeamos si existe. Sino lo insertamos | |
| if db.volcado.find( | |
| { 'fecha_iso': d['fecha_iso'], | |
| 'importe_raw': d['importe_raw'], | |
| 'concepto': d['concepto'] | |
| }).limit(1).count() == 0: | |
| insertData.append(d) | |
| if len(insertData)>0: | |
| db.volcado.insert(insertData) | |
| print 'Inserted elements: ' + str(len(insertData)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment