Selecting, filtering and aggregating in MongoDB if we have two dataset 1-First dataset contains population data of cities in the United States of America. Each record in the dataset has the following format:

{
     "city": "ACMAR",
     "loc": [
         -86.51557,
         33.584132
     ],
     "pop": 6055,
     "state": "AL",
     "_id": "35004"
}


Second dataset contains log data from a geo-locational service. Each record in the dataset has the following format:

{
    "_id": {
        "$oid": "5191f53b1b76a5666a8cbd64"
    },
    "status": 200,
    "requrest_time": 504,   // in milliseconds
    "type": "stationboard",
    "datetime": {
        "$date": "2014-02-19T07:03:32.000Z"
    }
}



how we can get all the states that have at least 2 cities with population > 1000000 (1M). And we need to  remember that city can be split into multiple records, and we should aggregate its population first. (zips dataset)
we need find a day (date) with the largest number of HTTP 500 errors (status=500).
how we can get all the states that have at least 2 cities with
  population > 1000000 (1M). And we need to remember that city can be
  split into multiple records, and we should aggregate its population
  first. (zips dataset)


I have zips collection database having 29353 records.

&gt; db.zips.find().count()
29353
&gt; db.zips.count()
29353


and the documents structure inside the collection is such as 

&gt; db.zips.find().pretty()
{
        "_id" : "01007",
        "city" : "BELCHERTOWN",
        "loc" : [
                -72.410953,
                42.275103
        ],
        "pop" : 10579,
        "state" : "MA"
}
{
        "_id" : "01002",
        "city" : "CUSHMAN",
        "loc" : [
                -72.51565,
                42.377017
        ],
        "pop" : 36963,
        "state" : "MA"
}
{
        "_id" : "01008",
        "city" : "BLANDFORD",
        "loc" : [
                -72.936114,
                42.182949
        ],
        "pop" : 1240,
        "state" : "MA"
}
{
        "_id" : "01011",
        "city" : "CHESTER",
        "loc" : [
                -72.988761,
                42.279421
        ],
        "pop" : 1688,
        "state" : "MA"
}
{
        "_id" : "01010",
        "city" : "BRIMFIELD",
        "loc" : [
                -72.188455,
                42.116543
        ],
        "pop" : 3706,
        "state" : "MA"
}


which is the same as mention documents structure by the OP.

{
     "city": "ACMAR",
     "loc": [
         -86.51557,
         33.584132
     ],
     "pop": 6055,
     "state": "AL",
     "_id": "35004"
} 


Suppose that here i want to write an aggregation query with a single match phase that filters for zipcodes with greater than 100,000 people.

Here i will write down the simple query with $match (aggregation) and Operator $pop to filter the documents.
$match Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

&gt; db.zips.aggregate([{$match:{pop:{$gt:100000}}}])
{ "_id" : "10021", "city" : "NEW YORK", "loc" : [ -73.958805, 40.768476 ], "pop" : 106564, "state" : "NY" }
{ "_id" : "10025", "city" : "NEW YORK", "loc" : [ -73.968312, 40.797466 ], "pop" : 100027, "state" : "NY" }
{ "_id" : "11226", "city" : "BROOKLYN", "loc" : [ -73.956985, 40.646694 ], "pop" : 111396, "state" : "NY" }
{ "_id" : "60623", "city" : "CHICAGO", "loc" : [ -87.7157, 41.849015 ], "pop" : 112047, "state" : "IL" }
&gt;


In the above example i have found out the 4 states and their respective cities . Where population is &gt; 100000.


  we need find a day (date) with the largest number of HTTP 500 errors
  (status=500).


For spherical queries, use the 2dsphere index result.

As MongoDB documented Here The use of 2d index for spherical queries may lead to incorrect results, such as the use of the 2d index for spherical queries that wrap around the poles.

MongoDB geospatial queries on  GeoJSON objects calculate on a sphere; MongoDB uses the WGS84 reference system for geospatial queries on GeoJSON objects.