将 ISODate 与 mongodb 查询和 node.js 结合使用

发布于 2025-01-16 15:30:10 字数 1989 浏览 1 评论 0原文

我正在创建一个字符串 - queryString - {"Project": "Serenity","DateOfWalkin": {"$gte": "2022-03-01" ,"$lt": "2022-03-31"} }

然后解析它 - queryObject = JSON.parse( queryString )

它解析成功,但是当我运行查询时它没有给我任何结果。

现在,当我使用 ISODate 更新两个日期的相同查询,然后尝试在 Mongo compass 中运行查询时,它工作正常。

我想使用 ISODate() 但问题是我构建 queryString 的方式是基于用户输入的不同条件,现在如果特定条件为真,那么然后我才想查询该特定字段。对于我正在搜索的其他字段,这些字段在我的数据库中属于“String”类型,我能够得到正确的结果,但到目前为止我面临着这个问题。

startDateendDate 是使用 HTML 中的日期选择器的用户输入

试用号 1

queryString = queryString + , "DateOfWalkin": {"$gte": ISODate("${startDate}")} ,"$lt": { "$date" : ISODate("${endDate}") } 这将给出解析器

错误,指出未找到标识符“I”,因此我无法解析该字符串。

为了解决这个问题,我尝试使用新的 Date 对象,尝试使用 toISOString 转换日期,但它不起作用。

试用号 2

queryString = queryString + ,"DateOfWalkin": {"$gte": "ISODate("${startDate}")"} ,"$lt": { "$date " : "ISODate("${startDate}")" } }

如果我们也对 ISODate 使用双引号,它将解决解析器问题,但是Mongo 查询不会给出任何结果,它将 ISODate 包装器视为字符串文字。

试用号 3

queryString = queryString + ,"DateOfWalkin": {"$gte": "${startDate}"} ,"$lt": { "$date" : "${ endDate}" } }

不给出结果

试验号 4

使用 new Date() 获取 ISODate 格式

查询变为 -

{
  Project: 'Serenity',
  DateOfWalkin: {
    '$gte': 'Tue Mar 01 2022 05:30:00 GMT+0530 (India Standard Time)',
    '$lt': 'Thu Mar 31 2022 05:30:00 GMT+0530 (India Standard Time)'
  }
}

仍然不给出任何结果

试用号 5

查询对象 -

{
  Project: 'Serenity',
  DateOfWalkin: {
    '$gte': { '$date': '2022-03-01' },
    '$lt': { '$date': '2022-03-01' }
  }
}

上面没有给出结果


有什么方法可以存储 ISODate("${startDate}") 的值并在中使用该变量主要的queryString

或者当我使用输入日期作为用户输入时是否有更好的方法来比较日期?

我尝试了多种方法,但此时感觉有点卡住了。

I am creating a string -
queryString - {"Project": "Serenity","DateOfWalkin": {"$gte": "2022-03-01" ,"$lt": "2022-03-31"} }

then parsing it - queryObject = JSON.parse( queryString )

It is parsing successfully, but when I run the query it does not give me any result.

Now when I update the same query by using ISODate for both dates, and then tried running the query in Mongo compass, it works fine.

I want to use ISODate() but the problem is the way I am building the queryString is based on different conditions from the user input, now if a particular condition is true then and only then I want to query that particular field. For other fields I am searching, which are of type "String" in my db, I am able to get the correct result, but for date I am facing this issue.

startDate and endDate are user inputs using date picker in HTML

TRIAL number 1

queryString = queryString + ,"DateOfWalkin": {"$gte": ISODate("${startDate}")} ,"$lt": { "$date" : ISODate("${endDate}") } }

This will give a parser error saying identifier "I" not found, hence I am not able to parse this string.

To combat this, I tried to use new Date object, tried to convert the date using toISOString, but it didn't work.

TRIAL number 2

queryString = queryString + ,"DateOfWalkin": {"$gte": "ISODate("${startDate}")"} ,"$lt": { "$date" : "ISODate("${startDate}")" } }

If we use double quotes for ISODate as well, it will solve the parser issue but Mongo query will not give any result and it is treating ISODate wrapper as a string literal.

TRIAL number 3

queryString = queryString + ,"DateOfWalkin": {"$gte": "${startDate}"} ,"$lt": { "$date" : "${endDate}" } }

Does not give result

TRIAL number 4

Using new Date() to get the ISODate format

Query becomes -

{
  Project: 'Serenity',
  DateOfWalkin: {
    '$gte': 'Tue Mar 01 2022 05:30:00 GMT+0530 (India Standard Time)',
    '$lt': 'Thu Mar 31 2022 05:30:00 GMT+0530 (India Standard Time)'
  }
}

Still it doesn't give any result

TRIAL number 5

Query object -

{
  Project: 'Serenity',
  DateOfWalkin: {
    '$gte': { '$date': '2022-03-01' },
    '$lt': { '$date': '2022-03-01' }
  }
}

Above doesn't give result


Is there any way I can store the value of ISODate("${startDate}") and use that variable in the main queryString?

Or is there a better way to compare dates when I am using the input dates as user input?

I have tried multiple approaches and kinda feeling stuck at this point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2025-01-23 15:30:10

为什么要解析字符串?

除了当前的问题之外,您还为 NoSQL- 打开了大门注射。您也可以直接像这样编写查询对象:

const queryObject = {};
queryObject["Project"] = "Serenity";
queryObject["DateOfWalkin"] = { $gte: ISODate("2022-03-01"), ["$lt"]: ISODate("2022-03-31") };

const ninDates = [];
ninDates.push(ISODate("2022-03-10"));
ninDates.push(ISODate("2022-03-20"));
queryObject["DateOfWalkin"]["$nin"] = ninDates;

db.collection.find(queryObject)

如果您确实坚持解析字符串,则使用 ejson< /a>

EJSON.parse('{ "Project": "Serenity", "DateOfWalkin": { "$gte": { "$date": "2022-03-01" }, "$lt": { "$date": "2022-03-01" } } }')

应该给出所需的查询对象。注意,键和值需要用双引号括起来("

Why do you parse the string?

Apart from your current problem, you also open the door for NoSQL-Injection. You can compose the query object also directly like this:

const queryObject = {};
queryObject["Project"] = "Serenity";
queryObject["DateOfWalkin"] = { $gte: ISODate("2022-03-01"), ["$lt"]: ISODate("2022-03-31") };

const ninDates = [];
ninDates.push(ISODate("2022-03-10"));
ninDates.push(ISODate("2022-03-20"));
queryObject["DateOfWalkin"]["$nin"] = ninDates;

db.collection.find(queryObject)

If you really insist to parse the string, then use ejson

EJSON.parse('{ "Project": "Serenity", "DateOfWalkin": { "$gte": { "$date": "2022-03-01" }, "$lt": { "$date": "2022-03-01" } } }')

Should give desired queryObject. Note, keys and values need to be enclosed by double-quotes (")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文