I come from SQL I don't have much (practically nothing) experience in MongoDB. I have the following data in a collection( ToDo ) in mongo:
{
"id": 1,
"description": "example description",
"tags": ["tag1", "tag2", "tag3"]
},
{
"id": 2,
"description": "lorem blablablab",
"tags": ["tag1","tag2"],
},
{
"id": 3,
"description": "lorem blablablab",
"tags": ["tag2", "tag3"],
},
{
"id": 4,
"description": "lorem blablablab",
"tags": ["tag2"],
},
{
"id": 5,
"description": "lorem blablablab",
"tags": ["tag3"]
}
seeing the data tags , which contains a list with elements of type string(tag1, tag2 and/or tag3), in SQL (and relating it correctly) you could do a count(tags) with a group by to count each of these values, that is, how many tasks are associated with each of the tags:
tag1 ---> 2
tag2 ---> 4
tag3 ---> 3
The conflict is how to replicate that type of query in mongo. I am looking for information to be able to work with the tag lists without having to analyze on the client side if the data is repeated or not. the idea is to optimize the operation by means of the query. It is using python and pyMongo.
Many thanks in advance...
Here is a possible solution:
This query would give you the following result:
I don't know if this output would work for you, if you needed it in an object you would have to merge the objects.
I hope this has been able to help you.
All the best