SELECT ID, country, states, state_map
FROM world
WHERE state_status = 'Y'
GROUP BY countryEach country should have multiple states.so states is a list.
could you please convert below Query into MongoDB Query
Hi,
You can perform an aggregation query to group by using Aggregation Pipeline. Normally it’s better to provide the sample document structure and the desired output from the query, instead of providing SQL query.
Without knowing exactly the document structure and the desired output, an example pipeline for your query would be:
db.world.aggregate([
{"$match": {"state_status": "Y"}},
{"$group": {"_id": "$country",
"ID": {"$addToSet": "$ID"},
"states": {"$addToSet":"$states"},
"state_map": {"$addToSet":"$state_map"},
}
},
])
am new to MongoDB
As mentioned by Robert, I would also recommend to enrol in a free online course at MongoDB University to learn more about MongoDB. Especially M121: The MongoDB Aggregation Framework, to learn how to use MongoDB’s Aggregation. The next session is 8th January 2019, there is plenty of time to register.
See also SQL to Aggregation Mapping Chart
Regards,
Wan.