How Select an entire array

Well, i’m new in Mongo and i’m trying to make a project, it’s basically an CRUD where you enter with what you’re doing now and when you started this job and when you will stop, then i create this model to my document:

db.Person.insert(

{

    "Name": name,

    "Job":{

        "Function": "",

        "Hour":{

            "Started":"",

            "Ended":""

        }

    },

    "College":{

        "Course": "",

        "Hour":{

            "Started":"",

            "Ended":""

        }

    },

    "Doing":[

            {

                "Activity":"",

                "Hour":{

                    "Started":"",

                    "Ended":""

                }

            }

        ]

}

);

So, my question it’s about how can i return (in python) just the entire ‘Doing’ array, i so tried so hard but the only way i can see to solve this, is using ‘projection’ and ‘agreggate’, but I think there is some easy way to solve this

Hi @Jose_Victor and welcome in the MongoDB Community :muscle: !

Would that work for you?

from pymongo import MongoClient

client = MongoClient()
db = client.get_database('test')
persons = db.get_collection('coll')
persons.drop()
persons.create_index('Name')
persons.insert_one(
    {
        'Name': 'name',
        'Job': {'Function': '', 'Hour': {'Started': '', 'Ended': ''}},
        'College': {'Course': '', 'Hour': {'Started': '', 'Ended': ''}},
        'Doing': [{'Activity': '', 'Hour': {'Started': '', 'Ended': ''}}]
    }
)
doc = persons.find_one({'Name': 'name'}, {'_id': 0, 'Doing': 1})
print(doc['Doing'])

Cheers,
Maxime.

1 Like

Man, you are an angel in earth i really thankfull by this, i didn’t know it’s possible to select like this, i’m so glad
:pray::pray::pray:

1 Like

I’m glad it was helpful to you :smiley: !

I recommend that you check out this M220P free training course on MongoDB University. :slight_smile:
I feel like you will learn a lot from it!

Cheers,
Maxime.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.