Reputation: 204
I making an API using MongoDB, Nodejs and ExpressJS.I have a collection called membership
const membershipSchema = new Schema(
{
membership_id: {
type: Number,
required: [true, "ID is required"],
unique: true,
},
member_detail: [{ type: Schema.Types.ObjectId, ref: "user" }],
validity: {
type: Number,
enum: [30, 90, 180, 365],
required: true,
},
fee: {
type: Number,
required: [true, "fee is required"],
},
registration_fee: {
type: Number,
required: [true, "registration fee is required"],
},
total_fee: {
type: Number,
required: [true, "total_fee fee is required"],
},
// status: {
// type: Boolean,
// required: [true, "status fee is required"],
// },
startDate: {
type: Date,
required: [true, "Start date is required"],
},
endDate: {
type: Date,
required: [true, "End date is required"],
},
added_by: { type: String, required: true },
last_update: {
type: Date,
default: Date.now,
},
}
);
After adding membership and requesting with GET, I get the following response
{
"member_detail": [
{
"_id": "5ea1e43dd401c828f4d1cf7d",
"name": "member421"
}
],
"_id": "5ea1e4a3d401c828f4d1cf7f",
"last_update": "2020-04-23T18:55:31.311Z",
"membership_id": 421,
"added_by": "admin",
"validity": 30,
"fee": 3500,
"registration_fee": 500,
"total_fee": 4000,
"startDate": "2020-04-18T06:21:04.869Z",
"endDate": "2020-07-18T06:21:04.869Z",
"createdAt": "2020-04-23T18:55:31.313Z",
"updatedAt": "2020-04-23T18:55:31.313Z",
"__v": 0
}
Now I am sending another GET request to membership collection with a query to test and to get the difference between two date (endDate - Current Date(24/04/2020)) and its working fine and gives following response
router.route("/alert").get((req, res) => {
Membership.aggregate(
[
{
$project: {
dayssince: {
$trunc: {
$divide: [
{ $subtract: ["$endDate", new Date()] },
1000 * 60 * 60 * 24,
],
},
},
},
},
],
(err, document) => {
if (err) {
return res.status(404).json({ error: true, message: err });
}
res.status(200).json({ data: document });
}
);
});
the above code returns me this result
{
"data": [
{
"_id": "5ea1e4a3d401c828f4d1cf7f",
"dayssince": 85
}
]
}
Now I want to merge the above result for every membership that I create
Expected output
{
"member_detail": [
{
"_id": "5ea1e43dd401c828f4d1cf7d",
"name": "member421"
}
],
"_id": "5ea1e4a3d401c828f4d1cf7f",
"last_update": "2020-04-23T18:55:31.311Z",
"membership_id": 421,
"added_by": "admin",
"validity": 30,
"fee": 3500,
"registration_fee": 500,
"total_fee": 4000,
"startDate": "2020-04-18T06:21:04.869Z",
"endDate": "2020-07-18T06:21:04.869Z",
"createdAt": "2020-04-23T18:55:31.313Z",
"updatedAt": "2020-04-23T18:55:31.313Z",
"__v": 0
**"dayssince": 85**
}
I've come accross aggregate function but I dont know how to implement with my query.If you need any other code from my project, feel free to comment.
Upvotes: 1
Views: 234
Reputation: 33933
Why request the DB just to calculate a time difference?
Once you have your first result... Why not just do the math right away?
// The result of the first query to DB
let firstResult = {
"member_detail": [
{
"_id": "5ea1e43dd401c828f4d1cf7d",
"name": "member421"
}
],
"_id": "5ea1e4a3d401c828f4d1cf7f",
"last_update": "2020-04-23T18:55:31.311Z",
"membership_id": 421,
"added_by": "admin",
"validity": 30,
"fee": 3500,
"registration_fee": 500,
"total_fee": 4000,
"startDate": "2020-04-18T06:21:04.869Z",
"endDate": "2020-07-18T06:21:04.869Z",
"createdAt": "2020-04-23T18:55:31.313Z",
"updatedAt": "2020-04-23T18:55:31.313Z",
"__v": 0
}
// NOW
let now = new Date();
// Create a date object from the endDate found in the DB result
let endDate = new Date(firstResult.endDate);
// Calculate the difference in rounded days
let result = Math.round((endDate-now) / (1000*60*60*24));
// Output
console.log(result)
// Then... Yes, you can add this to the initial response object
firstResult.dayssince = result;
console.log(firstResult)
Upvotes: 1