Search for entries that were posted in the previous month

Currently, I am working on a project with Meteor and have encountered a small issue. I believe that many of you may already have the solution.

In my setup, I am utilizing MongoDB.

My goal is to retrieve posts that were posted starting from the same date last month up until now.

For example:

today 3rd March 

I specifically want posts that were posted between

3rd Feb to 3rd March

I have inserted a date field in the document as follows:

var dat = new Date();

Is it possible to achieve this using the Date field alone, or do we need to store additional fields?

I am currently lacking ideas on how to accomplish this task.

Thank you in advance.

Answer №1

As stated in the official MongoDB documentation, you can use the following code snippet:

var end = new Date(); // current date
var start = new Date().setMonth(end.getMonth() - 1); // last month
db.posts.find({created_on: {$gte: start, $lt: end}});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for designating all content within a <div> element to open in a blank target (target="_blank")

I am looking to open all content within a specific <div> in a new tab (target="_blank"). I am open to any solution, whether it involves JS, CSS, HTML, classes, IDs, etc. I experimented with using <base target="_blank">, but it affect ...

Error: The "res.json" method is not defined in CustomerComponent

FetchData(){ this.http.get("http://localhost:3000/Customers") .subscribe(data=>this.OnSuccess(data),data=>this.OnError(data)); } OnError(data:any){ console.debug(data.json()); } OnSuccess(data:any){ this.FetchData(); } SuccessGe ...

Issue encountered when attempting to remove an element from an array in MongoDB

I'm currently working on removing an item from an array in a MongoDB model that I have created for countries. Below is my Country model: const CountrySchema = mongoose.Schema({ name: { type: String, required: true }, holidays: [ ...

javascript regular expression

Is there a way to extract the text that falls between the first colon and the first period in a string? s:4332Hhj4j32hh432kjh.EF4324rf46543DSVC3443 I attempted using the following regex: /:(.*?)\./g Unfortunately, it still captures the colon and p ...

Is there a way to apply styling to an SVG element using code before it is displayed using React?

Rendering my SVG in React is working fine render () { return ( <object type="image/svg+xml" data="./src/img/test.svg" ref="svg"> </object> ) } I am looking to allow the user to customize the fill color by selecting ...

Sorting through an array of objects using criteria from a separate array of objects

Currently, I am faced with the task of filtering an array of objects based on criteria from another array. Essentially, I am looking to refine a list based on filters selected by the user. To illustrate, let's consider my initial list of results: [ ...

The class name remains unchanged despite the change in value

I am currently working on a webpage that features two interactive tabs. The goal is to have one tab highlighted as "active" while the other remains inactive when clicked, and then switch roles when the other tab is selected. Below is the code snippet I ha ...

(Discord.JS) Bot failing to send direct message upon user joining the server

When attempting to send a DM message, I am using the following code: bot.on('guildMemberAdd', (member) => { member.send('a'); }); I am experiencing difficulty in understanding why the private message is not being successfully se ...

Troubleshooting problem with list rendering in a Nativescript-vue GridLayout

As a newcomer to nativescript, I am utilizing GridLayout in an attempt to optimize layout nesting for better performance. In my template, I have an image followed by a vertical list of four items next to it. However, only the first item on the list is visi ...

Utilizing Three.js for onWindowResize event parameterization

As I delve into the world of JavaScript coding and experiment with Three.js, I've come across a function that dynamically scales my scene based on the size of the browser window: function onWindowResize() { camera.aspect = window.innerWidth / wi ...

What might be causing my lightbox image to fail to load when I click on it?

Whenever I click on the image, the lightbox loads but the actual image fails to load, leaving a blank white box instead. Despite following a tutorial on YouTube that showed me how to create a lightbox step by step, I can't figure out what went wrong. ...

Tips on extracting the ID number prior to locating an element by its ID using Python Selenium

I am currently attempting to automate sending LinkedIn connection requests using Python with Selenium. However, I am facing an issue where the ember number value keeps changing every time I try to copy the id of the button. Sometimes it shows as #@id=" ...

Unable to execute jQuery - AJAX in PHP

Welcome_page.php <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#role").on("change", function() { alert($("#role").val()); var rol ...

What is the process for obtaining the sheetId using Google Sheets API with Javascript?

Hey there, if you need to use sortRange in the Google Sheets API, remember that sheetId should be an integer and not a string. How can I retrieve it starting with the sheet's name as a string? This method works perfectly for sorting my sheet. However ...

Retrieving a result from the reduce function in Angular

Is there a way to use the reduce function in order to return a list of objects? I am currently only able to return a single object with keys as project names and values as hours: { Name1: 9, Name2: 10, Name3: 30, } What changes can I make to my code to ac ...

Utilizing Angular partials within specific views with the assistance of ui-router

I am currently working on developing a MEAN application and facing some challenges while handling ui-router. Within my index.html file, I have set up the template for the entire website including a header, sidebar, and content area where I have placed < ...

`In NestJS Nested Schema, the @Prop decorator and mongoose options are not applied as expected

I'm currently working on constructing a Schema that includes a nested object. I am trying to define default values and required properties within the nested object, but it seems like the options I set are being ignored. task.entity.ts @Schema() expor ...

Guide on setting up and customizing run.json within the latest JetBrains Fleet for Next.js

I've been attempting to set up input/output for the latest IDE from JetBrains, Fleet. However, I've hit a roadblock and can't seem to figure it out on my own. That's why I'm turning to the Stack Overflow community for help - how do ...

How can I efficiently retrieve the name variable of a Quasar (or Vue 3) application?

Is there a way to incorporate something similar in Quasar without having to redefine the variable in every component? <template> <div>Enjoy your time at {{ APP_NAME }}.</div> </template> During the setup of my app with Quasar C ...

Changing the URL parameters to accommodate a specific query

Currently, I have set up the route as follows: app.get("/employees", (req, res) => { data.getAllEmployees().then((data) => { res.json(data); }).catch(function(err) { console.log("An error was encountered: " + err); }); }) ...