I am new on MongoDB and I am working on an application with MongoDB and C#. I am dealing with about 20 millions documents. I have nested objects in my collection. I need to write some queries to fetch some data based on some conditions on my second or third level of data in my collection. Is it good way to use unwind
command or I should solve my problem by using map
and reduce
? Does this approach will put me in trouble in future?
my data structure is like below
{
"_id" : ObjectId("5aef51e0af42ea1b70d0c4dc"),
"EndpointId" : "89799bcc-e86f-4c8a-b340-8b5ed53caf83",
"DateTime" : ISODate("2018-05-06T19:05:04.574Z"),
"Tags" : [
{
"Uid" : "E2:02:00:18:DA:40",
"Type" : 1.0,
"DateTime" : ISODate("2018-05-06T19:05:04.574Z"),
"Sensors" : [
{
"Type" : 1.0,
"Value" : NumberDecimal("-98")
},
{
"Type" : 2.0,
"Value" : NumberDecimal("-65")
},
{
"Type" : 3.0,
"Value" : NumberDecimal("7.845424441900629")
}
]
},
{
"Uid" : "12:3B:6A:1A:B7:F9",
"Type" : 1.0,
"DateTime" : ISODate("2018-05-06T19:05:04.574Z"),
"Sensors" : [
{
"Type" : 1.0,
"Value" : NumberDecimal("-95")
},
{
"Type" : 2.0,
"Value" : NumberDecimal("-59")
}
]
}
]
}
The EndpointId
and Tags.Uid
are not unique. I wrote some different queries on this collection for example a query to find a list of unique combination of EndpointId
and Tags.Uid
.
my query is as below
db.EndpointData.aggregate([
{ "$unwind" : "$Tags" },
{ "$group" : { "_id" : { "EndpointId" : "$EndpointId", "Uid" : "$Tags.Uid" },
"EndpointId" : { "$first" : "$EndpointId" },
"Url" : { "$first" : "$Url" },
"TagId" : { "$first" : "$Tags.Uid" },
"Type" : { "$first" : "$Tags.Type" },
"LastDate" : { "$max" : "$Tags.DateTime" },
"FirstDate" : { "$min" : "$Tags.DateTime" } } }
])
I have more queries like this. So is it recommended to use unwind
or it is better to use map
and reduce
?