MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Grup MongoDB berdasarkan rentang durasi

Memasukkan bagian-bagian ini ke posisi yang tepat dari $group._id . Anda dapat mencapai target Anda.

ampm : {
    $cond : {
        "if" : {
            $lt : [ {
                $hour : "$StartTime"
            }, 12 ]
        },
        "then" : "AM", 
        "else" : "PM"
    }
},
segment : {
    $let : {
        "vars" : {
            h : {
                $hour : "$StartTime"
            }
        },
        "in" : {
            $cond : {
                "if" : {
                    $lt: [ "$$h", 6 ]
                },
                "then" : "Night",
                "else" : {
                    $cond : {
                        "if" : {
                            $lt : [ "$$h", 12 ]
                        },
                        "then" : "Morning",
                        "else" : {
                            $cond : {
                                "if" : {
                                    $lt : [ "$$h", 18 ]
                                },
                                "then" : "Afternoon",
                                "else" : "Evening"
                            }
                        }
                    }
                }
            }
        }
    }
}

Agar lebih mudah dipahami, saya tempelkan keseluruhannya di sini

[
{
    "$match": {
        "Category": {
            "$in": ["Mobile"]
        },
        "StartTime": {
            "$gte": {"$date": "2014-01-29T00:00:00.000Z"},
            "$lt": {"$date": "2014-09-29T00:00:00.000Z"}
        }
    }
},
{
    "$group": {
        "_id": {
            "Category": "$Category", 
            "Country":"$Country",
            "City":"$City",
            "day": {
                "day":{"$dayOfMonth": "$StartTime"},
                "month":{"$month":"$month"},
                "year":{"$year":"$year"},

                // add this part to group by for each day of each month of each year
                ampm : {
                    $cond : {
                        "if" : {
                            $lt : [ {
                                $hour : "$StartTime"
                            }, 12 ]
                        },
                        "then" : "AM", 
                        "else" : "PM"
                    }
                },
                segment : {
                    $let : {
                        "vars" : {
                            h : {
                                $hour : "$StartTime"
                            }
                        },
                        "in" : {
                            $cond : {
                                "if" : {
                                    $lt: [ "$$h", 6 ]
                                },
                                "then" : "Night",
                                "else" : {
                                    $cond : {
                                        "if" : {
                                            $lt : [ "$$h", 12 ]
                                        },
                                        "then" : "Morning",
                                        "else" : {
                                            $cond : {
                                                "if" : {
                                                    $lt : [ "$$h", 18 ]
                                                },
                                                "then" : "Afternoon",
                                                "else" : "Evening"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


            }
        },
        "UniqueVisits": {
            "$sum": 1
        },
        "Date": {
            "$first": "$StartTime"
        }
    }
},
{
    "$project": {
        "_id": "$_id.Category",
        "Header": {
            "$concat": [{"$substr": [{"$month": "$Date"}, 0,2]},
                "/",
                {"$substr": [{"$dayOfMonth": "$Date"}, 0, 2]},
                "/",
                {"$substr": [{"$year": "$Date"}, 0, 4]}]
        },
        "Name": {
            "$concat": [
                {
                    "$ifNull": ["$_id.Country","notset"]
                },
                "~",
                {
                    "$ifNull": ["$_id.City","notset"]
                }
            ]
        },
        "UniqueVisits": "$UniqueVisits",            
    }
}
]



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. pilih hanya subdokumen atau array

  2. ECONNREFUSED kesalahan saat menghubungkan ke mongodb dari node.js

  3. Panduan MongoDB BSON

  4. Kapan Compound Indexes masih relevan di MongoDB 2.6, dengan adanya fitur Index Intersection yang baru?

  5. Menemukan entri N teratas dari Array