如何使用JavaScript在MongoDB中获取两个日期范围内的数据?

huangapple go评论70阅读模式
英文:

How to get Data between a range of two dates in MongoDB Using Javascript?

问题

我需要使用Nodejs API从我的MongoDB获取数据。我尝试以下代码:

const attendanceRecords = await Attendence.find({
  user: _id,
  currentDate: { $gte: fromDate, $lte: toDate },
}).populate("user", "firstname lastname email");

但它没有给我正确的数据。

我的MongoDB数据库中的currentDate字段是日期数据类型,我是使用以下代码存储这个日期的:

const mycurrentDate = new Date().toLocaleDateString('en-US', {
  day: '2-digit',
  month: '2-digit',
  year: 'numeric',
});

const formattedDate = new Intl.DateTimeFormat("en-US", optionss).format(date);

formattedDate的数据类型是字符串,现在我该如何筛选数据并获取两个日期范围内的数据?

英文:

I need to get data from my MongoDB using Nodejs API . I trying this

 const attendanceRecords = await Attendence.find({
      user: _id,
      currentDate: { $gte: fromDate, $lte: toDate },
    }).populate("user", "firstname lastname email");

But its not giving me correct data

currentDate field in my MongoDB database is date datatype and i am storing this date using this code

const mycurrentDate = new Date().toLocaleDateString('en-US', {
    day: '2-digit',  
    month: '2-digit',
    year: 'numeric',
 });
 
 const formattedDate = new Intl.DateTimeFormat("en-US", optionss).format(date); 

the datatype of formattedDate is string Now how can I filter data and get data between a range of two dates ?

答案1

得分: 1

这个错误很可能是因为尝试将日期类型存储为字符串类型,并在查询时将它们作为日期类型进行过滤。

const mycurrentDate = new Date(); // 当你存储到 mongodb 时,应该使用日期类型

let fromDate = new Date('2023-06-22'); // 使用日期类型进行过滤
let toDate = new Date('2023-06-31'); // 日期类型
const attendanceRecords = await Attendence.find({
      user: _id,
      currentDate: { $gte: fromDate, $lte: toDate },
    }).populate("user", "firstname lastname email");
英文:

This error is likely caused by trying to store Date type as string type and filtering them as Date type during the query. 

const mycurrentDate = new Date(); // when you store to mongodb you should use Date type

let fromDate = new Date('2023-06-22'); // use Date type to filter
let toDate = new Date('2023-06-31'); // Date type
const attendanceRecords = await Attendence.find({
      user: _id,
      currentDate: { $gte: fromDate, $lte: toDate },
    }).populate("user", "firstname lastname email");

答案2

得分: 0

假设在Node.js中使用Mongoose进行查询,查询将如下所示:

var startDate = new Date("2023-01-01");
var endDate = new Date("2023-12-31");

Modal_name.find({
  date_field_name: {
    $gte: startDate,
    $lte: endDate
  }
});

其中,date_field_name 是你在集合中创建的 created_at 字段的名称。

英文:

Assume using Mongoose with node, the query will be like that!

> var startDate = new Date("2023-01-01");
var endDate = new Date("2023-12-31");

Modal_name.find({
  date_field_name: {
    $gte: startDate,
    $lte: endDate
  }
});

date_field_name your created_at field name in Collection

huangapple
  • 本文由 发表于 2023年6月22日 17:40:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定