lundi 16 août 2021

Pymongo get random record and element of the record

Hello Guys i have a record like this in my mongoDB

{
_id: "611ae422a01534cecce5533d"
firstname:"Test1"
lastname: "Test2"
}

This is my code

res = firstnameCollection.aggregate([{'$sample': {'size': 1 }}])
print(list(res))

i want to get a random record out of my database, i have many records like i mentioned above. the output i get ist a list of the record, when i remove the list attribute i get:

<pymongo.command_cursor.CommandCursor object at 0x0000021078B82EB0>

but i just want to output the firstname of the element:

Test1



Aucun commentaire:

Enregistrer un commentaire