Insert document in MongoDB using pymongo
NickName:Bruce Espeschit Ask DateTime:2018-07-20T20:27:07

Insert document in MongoDB using pymongo

I'm using Python for the first time to run a crawler. I've got the crawler to work and now I want to save the results in my MongoDB using pymongo,but for some reason I get this error: "NameError: name 'city' is not defined". If I wrap city in quotes,it works as expected,but I want to save it as it is. Anyone has done something similar or know what the solution is?

def gotHolidays(self, response):
        cityName = response.meta['name']
        feriado = []
        facult = []
        for selector in response.css("span.one"):
            feriado.append(selector.css("::text").extract())

        for selector in response.css("span.two"):
            facult.append(selector.css("::text").extract())

        city = {
            'city': cityName,
            'holidays':{
                'facult': facult,
                'feriado': feriado                
            }
        }
        print(json.dumps(city))

    from pymongo import MongoClient
    client = MongoClient()
    client = MongoClient ('localhost', 27017)
    db = client['myBank']
    myCollection = db.myCollection
    myCollection_data = {
        'cities': city
    }
    result = myCollection.insert_one (myCollection_data)

Copyright Notice:Content Author:「Bruce Espeschit」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51442558/insert-document-in-mongodb-using-pymongo

More about “Insert document in MongoDB using pymongo” related questions

How can I insert a document containing integer field in Mongodb with document validation using pymongo?

I am trying to insert a document in MongoDB having document validation using pymongo. It has a type validator to check for data types. db.createCollection( "final", { validator: { $and: [ ...

Show Detail

Insert document in MongoDB using pymongo

I'm using Python for the first time to run a crawler. I've got the crawler to work and now I want to save the results in my MongoDB using pymongo,but for some reason I get this error: "NameError: n...

Show Detail

insert_many or insert_one not working in a MongoDB in PyMongo

I am trying to use the insert_one function or the insert_many function to Add a Document to my Collection in the MongoDB , I keep seeing the below error , I am trying the Dictionary approach for th...

Show Detail

Insert a NumPy rec.array to MongoDB using PyMongo

In an other question some people are trying to insert a Pandas DataFrame into MongoDB using Python internal structures (dict, list) Insert a Pandas Dataframe into mongodb using PyMongo I wonder if...

Show Detail

Can't insert into MongoDB with Pymongo

I'm trying to insert data into a MongoDB database. I'm using the following minimum working example: import pymongo conn = pymongo.Connection() db = conn.xxx db.xxx.insert({'foo': 'bar'}) The data

Show Detail

insert array into mongodb using pymongo

I am trying to add array into mongdb using pymongo I have another program that will return something like ['1 aksdfjas;dkfjsa;dfkj','2 ;alksdjf;askdjf;asdfjkasdf', '3 ;alksdfj;asdlkfj;asdfj'] a...

Show Detail

Bulk insert mapped array using pymongo fails due to BulkWriteError

I am trying to bulk insert documents in MongoDB using python library pymongo. import pymongo def tryManyInsert(): p = {'x' : 1, 'y' : True, 'z': None} mongoColl = pymongo.MongoClient('loca...

Show Detail

Extract Timestamp of latest document in MongoDB using Pymongo

I have mongoDB collection with columns filename and text I want to extract timestamp of latest document using pymongo What I Tried: from pymongo import MongoClient host = "127.0.0.1:27017" clien...

Show Detail

Query mongoDB by date document created using pymongo

Lots of solutions to querying mongoDB using date/time field in MongoDB but what if the mongo doc doesn't have a date/time field? I've noticed that when I hover the mouse over a document _id (using

Show Detail

How to insert a document with null value into mongodb using pymongo?

I have a .csv file with a few cells set to null and am trying to store this data in mongodb using pymongo. However I will be feeding this data to an API so I need the fields to be null instead of a

Show Detail