0
votes

i am new in mongodb. i am creating free database using mongolab. my database name is enron and this database i have create on collection name is mbox. my system have one json file.i am trying to import this json file into collection using python.my database connection connect sucessfully but importing json file then error occure.please give me proper solution.

 import os
 import sys
 import envoy
 from bson import json_util # Comes with pymongo
 from pymongo import MongoClient


 client = pymongo.MongoClient('mongodb://user:[email protected]:33499/enron')
 r = pymongo.MongoClient('mongoimport --host mongolab.com --port 033499 --username    pramod_jadhav --password p9822581103 --collection mbox --db enron --file C:\Users\sachin\Documents\IPython \ch06-mailboxes\data\enron.mbox.json')
 print 'json import sucessfully'
1
This is your client library pymongo. You can't run a command line program with it. Your first statment is a connection for your code to use, and the argument is a connection string. You don't call mongoimport in this way. Just run it from the command line. - Neil Lunn
how to connect shell for free remote database - pramod24
Question is low quality and shows no research - Neil Lunn

1 Answers

2
votes

Here, we need to parse url so that it can handle special char(like @) correctly. For that import urllib and use method urllib.parse.quote([mongo _lab_url]),

In your case,

import urllib

db_url = urllib.parse.quote('mongodb://user:[email protected]:33499/enron')

client = MongoClient(db_url)

should work !!!