Created
May 20, 2022 09:09
-
-
Save fzn0x/dabcc5a18ee9a32eb29ac4e298e89e69 to your computer and use it in GitHub Desktop.
Group data by dates in mongodb without extract all the values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongoose from "mongoose"; | |
import dotenv from "dotenv"; | |
dotenv.config(); | |
mongoose | |
.connect(process.env.MONGO_SOURCE) | |
.then(async () => { | |
console.log("connected"); | |
let db = mongoose.connection.db; | |
const data = await db | |
.collection("cyball_guild_leaderboards") | |
.aggregate([ | |
{ | |
$group: { | |
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } }, | |
list: { $push: "$$ROOT" }, | |
count: { $sum: 1 }, | |
}, | |
}, | |
]) | |
.toArray(); | |
console.log(data); | |
}) | |
.then(() => { | |
console.log("process successful"); | |
}) | |
.catch((e) => { | |
console.log("process failed:", e.message); | |
}) | |
.then(() => { | |
console.log("disconnecting"); | |
mongoose.disconnect(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment