Snippets Collections
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const userSchema = new Schema(
  {
    username: {
      type: String,
      required: true,
      unique: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
    password: {
      type: String,
      required: true,
    },
    isAdmin: {
      type: Boolean,
      default: false
    }
  },
  { timestamps: true }
);

module.exports = User = mongoose.model("User", userSchema);


// @routes  GET api/user/stats
// @desc    Get how many users are registered with in a year/month/week
// @access  Private - Admin
router.get("/stats", isAuthenticatedAndAdmin, async (req, res) => {
  User.aggregate([
    // { $match: { createdAt: { $gte: currentYear } } },
    {
      $project: {
        year: { $year: "$createdAt" },
        month: { $month: "$createdAt" },
        week: { $week: "$createdAt" },
      },
    },
    {
      $group: {
        _id: { year: "$year", month: "$month", week: "$week" },
        count: { $sum: 1 },
      },
    },
  ])
    .then((userStats) => res.status(200).json(userStats))
    .catch((err) => res.status(500).json(err));
});
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { ObjectId } = mongoose.Schema.Types;

const orderSchema = new Schema(
  {
    userId: {
      type: ObjectId,
      ref: "User",
    },
    productId: {
      type: ObjectId,
      ref: "Product",
    },
    size: {
      type: String,
      required: true,
    },
    color: {
      type: String,
      required: true,
    },
    quantity: {
      type: Number,
      default: 1,
    },
    totalPrice: {
      type: Number,
      required: true,
    },
    paymentMethod: {
      type: String,
      default: "Stripe",
    },
  },
  { timestamps: true }
);

module.exports = Order = mongoose.model("Order", orderSchema);


// @routes  GET api/order/income
// @desc    Get monthly income
// @access  Private - Admin
router.get("/income", isAuthenticatedAndAdmin, (req, res) => {
  const date = new Date();
  const lastMonth = new Date(date.setMonth(date.getMonth() - 1));
  const previousMonth = new Date(date.setMonth(lastMonth.getMonth() - 1));

  Order.aggregate([
    { $match: { createdAt: { $gte: previousMonth } } },
    { $project: { month: { $month: "$createdAt" }, sales: "$amount" } },
    { $group: { _id: "$month", total: { $sum: "$sales" } } },
  ])
    .then((income) => res.status(200).json(income))
    .catch((err) => res.status(500).json(err));
});
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { ObjectId } = mongoose.Schema.Types;

const reviewSchema = new Schema(
  {
    productId: {
      type: ObjectId,
      ref: "Product",
    },
    reviews: [
      {
        userId: {
          type: ObjectId,
          ref: "User",
        },
        rating: {
          type: Number,
          required: true,
        },
        comment: {
          type: String,
        },
      },
    ],
    totalRate: {
      type: Number,
      default: 0,
    },
  },
  { timestamps: true }
);

router.get("/:id", (req, res) => {
  Review.findOne({ productId: req.params.id })
    .populate({
      path: "reviews",
      populate: { path: "userId", select: "-_id username" },
    })        
    .then((reviews) => {      
        res.status(200).json(reviews);      
    })
    .catch((err) => res.status(500).json(err));
});
star

Sun Mar 20 2022 11:24:15 GMT+0000 (UTC)

#mongo #nodejs
star

Sun Feb 21 2021 02:49:49 GMT+0000 (UTC) https://stackoverflow.com/questions/42462992/morphia-complex-mongodb-aggregate-substr-project-sort-etc

#mongo #morphia
star

Thu Oct 22 2020 14:19:46 GMT+0000 (UTC) http://learnmongodbthehardway.com/schema/timeseries/#notes

#mongo #timeseries #schema

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension