I have a collection with two or more objects – that come from two different resources:
[
{
_id: ObjectId("123ab5fc3065ff2337479d89"),
samples: [
{
Set1: {
"latitude": "12.123"
},
Set2: [
{
params: {"device": "456"}
}
]
}
]
},
{
_id: ObjectId("123ab4fc3065ff2337479d89"),
samples: [
{
Set1: {
params: {"device": "123"}
}
}
]
}
]
The issue with this data set is that it comes from different devices but store the same list of items in different properties. The first object has a list of samples with set1 and set2 with different types. The second document has set1 only but has identical properties to set2 in the first object.
So I have to fix the data and reorganize it – my assumption is that the data only comes in this way. My steps to achieve the restructuring is that I need to rename sets to appropriate data that is being stored – ie if it has location data stored rename set1 to location. Then bring all these objects to the root
//desired results
[
{
_id: ObjectId("123ab5fc3065ff2337479d89"),
samples: {
device: { params: {"device": "456"} },
location: {"latitude": "12.123" } }
},
{
_id: ObjectId("123ab4fc3065ff2337479d89"),
samples: {
device: { params: {"device": "123"} } } }
]
I started this query out by $unwinding the samples but then get a little stuck because of the structure it will lose on object if it doesn’t match so then I decided to add in $facet. Is this the right direction the get the results? Basically my goal is to clean up the data so that the aggregations are only applied to the specific objects that exists and also do not duplicate the data nor do they reduce the data because the pattern doesn’t match.
db.getCollection("collection").aggregate(
[
{
$unwind: "$samples"
},
{
$facet: {
set2: [{
$unwind: "$samples.set2"
}],
set1: [
]
}
}