mongoose

How can I set the TTL date on document creation in Mongoose?

泄露秘密 提交于 2021-01-04 07:35:13
问题 I am trying to make a promoCode schema in Mongoose. On creation, I need to be able to set the promo code's expiration date. Promo codes do not necessarily have the same TTL . I looked at this question, but my documents still aren't expiring. This is my promoCode.js file: var mongoose = require("mongoose"); var promoCodeSchema = mongoose.Schema({ expirationDate: Date, createdAt: { type: Date, expireAfterSeconds: Number, default: Date.now } }) module.exports = mongoose.model("Promo",

How can I set the TTL date on document creation in Mongoose?

不羁的心 提交于 2021-01-04 07:35:08
问题 I am trying to make a promoCode schema in Mongoose. On creation, I need to be able to set the promo code's expiration date. Promo codes do not necessarily have the same TTL . I looked at this question, but my documents still aren't expiring. This is my promoCode.js file: var mongoose = require("mongoose"); var promoCodeSchema = mongoose.Schema({ expirationDate: Date, createdAt: { type: Date, expireAfterSeconds: Number, default: Date.now } }) module.exports = mongoose.model("Promo",

Aggregation with multiple criteria's and sum match based on the count key

房东的猫 提交于 2021-01-04 07:34:45
问题 I was trying get to get the count of element that is present in the array of objects in another collection. Example: Collection A: { _id:1, name:"Sample1" } { _id:2, name:"Sample 2" } { _id:3, "name":"Sample 3" } { _id:4, "name":"Sample 4" } Collection B: { _id:11, items:[ {_id:1, name:"sample1",size:1},{_id:3, name:"sample 3",size:5}] } { _id:12, items:[ {_id:1, name:"sample1",size:2},{_id:3, name:"sample 3",size:6}] } { _id:13, items:[ {_id:2, name:"sample2", size:5},{_id:1, name:"sample 1"

Aggregation with multiple criteria's and sum match based on the count key

痴心易碎 提交于 2021-01-04 07:34:42
问题 I was trying get to get the count of element that is present in the array of objects in another collection. Example: Collection A: { _id:1, name:"Sample1" } { _id:2, name:"Sample 2" } { _id:3, "name":"Sample 3" } { _id:4, "name":"Sample 4" } Collection B: { _id:11, items:[ {_id:1, name:"sample1",size:1},{_id:3, name:"sample 3",size:5}] } { _id:12, items:[ {_id:1, name:"sample1",size:2},{_id:3, name:"sample 3",size:6}] } { _id:13, items:[ {_id:2, name:"sample2", size:5},{_id:1, name:"sample 1"

How to get the documents based on Date Filters(Week, Month and Custom Dates) in MongoDB?

狂风中的少年 提交于 2021-01-04 06:37:14
问题 I am trying to fetch all the documents based on date filters as below in MongoDB: This Week only Last Week only This Month only Last Month only Between Custom Dates. My collection: [ { _id: "123", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "223", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "474", status: "unseen", userId: "589", createdAt: ISODate("2020-11-10T04:35:29.646Z"), }, { _id: "875", status:

How to get the documents based on Date Filters(Week, Month and Custom Dates) in MongoDB?

Deadly 提交于 2021-01-04 06:36:30
问题 I am trying to fetch all the documents based on date filters as below in MongoDB: This Week only Last Week only This Month only Last Month only Between Custom Dates. My collection: [ { _id: "123", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "223", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "474", status: "unseen", userId: "589", createdAt: ISODate("2020-11-10T04:35:29.646Z"), }, { _id: "875", status:

mongodb aggregate $lookup vs find and populate

房东的猫 提交于 2021-01-04 04:57:10
问题 I have a Video Schema like this: const VideoSchema = new mongoose.Schema({ caption: { type: String, trim: true, maxlength: 512, required: true, }, owner: { type: mongoose.Schema.ObjectId, ref: 'User', required: true, }, // some more fields comments: [{ type: mongoose.Schema.ObjectId, ref: 'Comment', }], commentsCount: { type: Number, required: true, default: 0, }, }, { timestamps: true }); and a simple Comment schema like this: const CommentSchema = new mongoose.Schema({ text: { type: String,

mongodb aggregate $lookup vs find and populate

天涯浪子 提交于 2021-01-04 04:54:28
问题 I have a Video Schema like this: const VideoSchema = new mongoose.Schema({ caption: { type: String, trim: true, maxlength: 512, required: true, }, owner: { type: mongoose.Schema.ObjectId, ref: 'User', required: true, }, // some more fields comments: [{ type: mongoose.Schema.ObjectId, ref: 'Comment', }], commentsCount: { type: Number, required: true, default: 0, }, }, { timestamps: true }); and a simple Comment schema like this: const CommentSchema = new mongoose.Schema({ text: { type: String,

model.save() returns an invalid output

白昼怎懂夜的黑 提交于 2021-01-03 06:24:32
问题 I am making a simple test for RESTApi using Node.js, mongodb and express from this article: MERN Part I: Building RESTful APIs with Node.js and Express but there is an error somewhere in code i can't locate. The author of article used babel but due to some other error i avoided it. Given below are code files: App.js var routes= require('./src/routes/userRoutes').routes var express= require("express") var mongoose=require('mongoose') var bodyParser=require('body-parser') const app = express();

Easiest way to set up a post-find transformation hook in Mongoose

断了今生、忘了曾经 提交于 2021-01-02 08:40:36
问题 I am new to Mongoose and have been given a project to extend. I quickly grasped the concept of pre and post hooks, but was wondering why there are no such hooks for find , but only for save and delete . What would be the easiest way to set up some transformations on the retrieved objects? Of course, I want to do this at the model level, and not do it every time I retrieve some objects. I found this plugin: https://www.npmjs.com/package/mongoose-post-find and I think it will do the job well,