Pymongo: How to insert an array of multiple fields in a document?

12,225

This should give you the idea how to do it

import pymongo

client = pymongo.MongoClient('yourHost', 30000) # adjust to your needs
db = client.so
coll = db.yourcollection

# show initial data
for doc in coll.find():
    print(doc)

# update data
places_visited = [
    "Palace of Dob",
    "Palace of Victoria",
    "Sahara Desert"
]
coll.update({}, { "$set": { "Places Visited": places_visited } }, multi=True)

# show updated data
for doc in coll.find():
    print(doc)  

which for your sample data should give output similar to this

daxaholic$ python3 main.py 
{'name': 'Bunty', 'Basic Intro': 'A.B.C.D.', '_id': '4564345343'}
{'name': 'Humty', 'Basic Intro': 'B.C.D.', '_id': '45657865745'}
{'name': 'Bunty', 'Places Visited': ['Palace of Dob', 'Palace of Victoria', 'Sahara Desert'], 'Basic Intro': 'A.B.C.D.', '_id': '4564345343'}
{'name': 'Humty', 'Places Visited': ['Palace of Dob', 'Palace of Victoria', 'Sahara Desert'], 'Basic Intro': 'B.C.D.', '_id': '45657865745'}  

For further information see the docs about update

Share:
12,225
Amar
Author by

Amar

Updated on June 28, 2022

Comments

  • Amar
    Amar almost 2 years

    I am working on MongoDB in python [pymongo]. I want to insert an array of multiple fields in a document. For example: In the below structure of a collection, I want to insert array of Places Visited in all documents. I do not know what it is called in the world of Mongo.So that I may insert it. How to insert an array in a document? Can some one help?

    collectionName
        {
                "_id" : "4564345343",
                "name": "Bunty",
                "Basic Intro": "A.B.C.D.",
                "Places Visited": [
                        "1" : "Palace of Dob",
                        "2" : "Palace of Victoria",
                        "3" : "Sahara Desert"
                ]
        }
        {
                "_id"  : "45657865745",
                "name": "Humty",
                "Basic Intro": "B.C.D.",
                "Places Visited": [
                        "1" : "Palace of Pakistan",
                        "2" : "Palace of U.S.A."
                        "3" : "White House"
                ]
        }
    
  • Amar
    Amar over 7 years
    Can you elaborate this line? coll.update({}, { "$set": { "Places Visited": places_visited } }, multi=True) 1. Empty Braces {} 2. $set 3. multi=true and the most important the flow of braces.
  • DAXaholic
    DAXaholic over 7 years
    The first is the query part of the update, empty dict means simply match all documents - you can adjust that of course to your needs but you didn't mention any query needs in your question so it is empty. $set is the operator used to set a field in the document. I updated my answer to include a link to the official documentation of mongoDB
  • Amar
    Amar over 7 years
    The last one is, what do you mean by "Match all the documents" and "Query needs"? It will be appreciative if you give example of these.
  • DAXaholic
    DAXaholic over 7 years
    Well you have to specify what documents to update - usually you don't want to update all the docs in the collection but only some of them and you have to select them with that query paramter. If you pass {} it simply matches all docs so all docs are updated. Query needs --> simply your needs - I don't know the needs of your application; you may want to update all documents but maybe you don't. In the latter case you have to specify a query as mentioned previously to select the subset of docs to update. You can find examples in the documentation - SO is no place to write tutorials ....