Reputation: 328
I have a collection called tabelas with data
such as:
{ "_id" : ObjectId("5cdcd4669dc6d6274f2234dc"),
"id" : "recdj5bcTznLBGtbY",
"fields" : { "id_tabela" : "tab0005" },
"createdTime" : "2018-08-13T19:15:26.000Z" }
I am trying to create an array with just the values given by "id_tabela"
, in this case, my result would be ["tab0005"]
.
I tried, using pymongo to get first the "fields"
values and later to get the id_tabela
values. But I am not even getting the fields
values.
tabelas = db.get_collection("tabelas")
db.drop_collection("tabelas")
result_tabelas = tabelas.insert_many(lista_tabs)
tabelas = db.get_collection("tabelas")
tab_array = list(tabelas.find())
vec_fields = tab_array['fields']
The last line results in an error of the kind:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
Upvotes: 1
Views: 229
Reputation: 46461
You can use aggregation
as well
db.collection.aggregate([
{ "$group": {
"_id": False,
"array": {
"$addToSet": "$fields.id_tabela"
}
}}
])
Upvotes: 1