0

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?

Mhmd
  • 15
  • 6
  • Probably a little too broad for this site. Remember, the first thing to do is write a naive implementation and only then attempt to optimize your solution. You don't even have any benchmarks. What does your research indicate? What have you tried? –  May 22 '18 at 17:02
  • @jdv I put some more details in my question. I think it helps more. – Mhmd May 22 '18 at 17:41
  • Everything you are asking here as actually addressed in the answer given to your last question. I suggest you read the large section of text and not just use the code. There is detailed explanation there about "when" you **need** to use `$unwind` and when **you don't**. There is no such thing as "recommend" other than if you do not "need" to then "don't". That really is the only point that matters to the context of the question here, and there as well. – Neil Lunn May 22 '18 at 22:24
  • The brief of what is already covered is "if your data requires" you to actually accumulate things from "inside the array" then you have to `$unwind`. The quick "litmus test" is run the two versions of aggregation statements on your data. If both return the same results, then you don't need `$unwind`. If they do differ, then you do. Recommendations or best practice have nothing to do with this. – Neil Lunn May 22 '18 at 22:27
  • I am confused on using unwind and map reduce. by unwinding the data I will have memory problem and time of query is very bad. by using map reduce I will not have memory problem but it takes long time to calculate inside all of my nested documents. I must decrease the time of query. This makes me confused. – Mhmd May 22 '18 at 22:57

0 Answers0