Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Created May 20, 2022 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzn0x/dabcc5a18ee9a32eb29ac4e298e89e69 to your computer and use it in GitHub Desktop.
Save fzn0x/dabcc5a18ee9a32eb29ac4e298e89e69 to your computer and use it in GitHub Desktop.
Group data by dates in mongodb without extract all the values.
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