How to query for a null fields with graphql? - javascript

I'm trying to query for a field that can be null or its _gt, but cannot find a way to make this work.
I've tried this:
query GetBanner {
foo(
sort: "posicao:asc",
where: {
startDate_lt: "2021-10-13T21:13:16.510Z",
_or: [
{ endDate_gt: "2021-10-13T21:13:16.510Z"},
{ endDate: null }
]
}
) {
id
created_at
updated_at
startDate
endDate
}
}
But I'm getting the following error message:
Invalid format, expected a timestamp or an ISO date
How to properly query this?

Related

Mongodb+mongoose update documents with date from other field = cast to date error

okay so i have some documents that are missing some data, im trying to update them like this;
await MyModel.updateMany(
{ imported: null },
{ $set: {
"imported": "$post.date"
}}
)
but i get cast to date error.
so i decided to work with a single document to figure out why that is.
so first lets see what inside post.date
console.log(
await MyModel.find({ _id: '5c2ce0fa527ad758bdb29506' })
.select('post.date imported')
)
[
{
post: { date: 2018-11-29T18:02:25.000Z },
_id: new ObjectId("5c2ce0fa527ad758bdb29506")
}
]
okay, so i though what happens if i copy the date and hardcode it, like so:
await MyModel.updateMany(
{ _id: "5c2ce0fa527ad758bdb29506" },
{ $set: {
"imported": "2018-11-29T18:02:25.000Z"
}}
)
that works just fine, but when i try to use the value from $post.date, like this:
await MyModel.updateMany(
{ _id: "5c2ce0fa527ad758bdb29506" },
{ $set: {
"imported": "$post.date"
}}
)
it results in
Cast to date failed for value "$post.date" (type string) at path "imported"
i have tried to use $dateFromString and $toDate but with no luck, but i feel like mongoose is trying to validate the string value "$post.date" and not interpret it as mongo would even through the mongoose schema has no validators defined.
so i tried adding { runValidators: false }, but it feels like it is ignored
await MyModel.updateMany(
{ _id: "5c2ce0fa527ad758bdb29506" },
{ $set: {
"imported": "$post.date"
}},
{ runValidators: false }
)
as the error is the same.
im running mongoose v. 6.2.0 and mongodb v. 4.2.18
any inputs are much appreciated
You can use this
db.collection.update({
imported: null
},
[ //aggregate update
{
"$set": {
"imported": "$post.date"
}
}
])
Supported from Mongo 4.2+

How to find and replace all 'null' date occurences in MongoDB collection?

Also replacing other formats later, such as: finding all dates with 'dd/mm/YYYY' format and changing them to ISO 'YYYY-mm-dd' format.
But as for the 'null' issue, this is the .js I am trying to run in MongoDB (NoSQLBooster):
use sms
db.collection1.find({
"FirstDate":null
})
.projection({})
//.sort({ _id: -1 })
//.limit(1000)
.forEach(function(doc) {
var date = doc.FirstDate
if (date == null) {
date = ''
}
})
And all I'm getting is "undefined" results. What could the problem be?
You can use update with {multi: true} to ensure all values that match are updated (not only the first one)
db.collection.update({
FirstDate: null
},
{
$set: {
FirstDate: ""
}
},
{
multi: true
})
Example here

How to find if all dates in a array are lesser than current date using loopback 3 filters?

I have an object in my databank which has a array of blocked dates, and I want to create a loopback filter function which retrieves the instances that have expired all their values (IE: all values are lesser than the current date).
I have tried the following query, but it didn't work:
const query = {
where: {
blockedDates: {
nin: [
{ lt: date }
]
}
},
fields: "id"
}
For date filtering, inside where you can pass object with lt or gt defined. For example:
const query = {
where: {
blockedDates: {lt: Date.now()}
}
}

convert sql query to sequelize query

how to convert this to sequelize query . I've tried using between but it does not have the same result. I dont think between is the solution , how about lte and gte ? Thank you
using between
where: {
createdAt: {
[Op.between]: ["2018-07-08T14:06:48.000Z", "2019-10-08T22:33:54.000Z"]
}
}
convert this sql to sequelize query
SELECT * FROM testtable WHERE createdAt >= '2020-06-15 00:00:00' AND createdAt <= '2020-06-18 00:00:00'
You can include the same property more than once within the where and this would work like AND in your SQL query:
where: {
createdAt: { [Op.gte]: '2020-06-15' },
createdAt: { [Op.lte]: '2020-06-18' }
}

Cannot get collections by date with mongoose

I can not query the collection filtering by a date
The following code is that I have so far
//The following code shows how my schema is:
date: {type: Date, required: true}
//This is a date from a collection in MongoDB:
date: 2019-09-06T16:48:14.000+00:00
//This is how I saved the date in the MongoDB:
"2019/09/09 08:55:15"
//And this is how I perform the query in NodeJS:
let year = req.query.year;
let month = req.query.month;
let day = req.query.day;
let startDate = new Date(year, month, day);
let endDate = new Date(year, month, day);
// find documents in MongoDB
let query = SALES.find({ date: { $gte: startDate, $lte: endDate }});
// execute the query at a later time
query.exec(function (err, item) { // item is a dictionary
if (err) return handleError(err); // throws an error if any
if (item === null || item.length === 0) {
res.json({
status: 'empty',
});
}
else {
res.json({
salesRecord: item
});
}
});
I read that is easy to get, but I am not able to do it. Any help is welcome 🙏
I have not error on the query, simply I get the response as empty.
The expected results is to get the dates from the specified date
Short answer
Date you saved("2019/09/09 08:55:15") is being treated as a string.
Try this:
db.mycollection.find({
"date" : {"$gte": new Date()}
})
or
db.mycollection.find({
"date" : {"$gte": ISODate(new Date())} // current date
});
Description
Mongodb shell provides various methods to return the date.It can be a string or as a Date object:
Date() method which returns the current date as a string.
new Date() constructor which returns a Date object using the ISODate() wrapper.
ISODate() constructor which returns a Date object using the ISODate() wrapper.

Categories

Resources