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
Related
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?
I have a creation date attribute of a document which has a format like this: "05/03/2020" of type String.
I must extract all the documents from my system having a creation date before "05/03/2020".
I tried with:
db.MyCollection.find ({DateC: {$lte: "05/03/2020"}})
but it still returns 0.
Is there a way to compare if a date format String is before another date?
Thank you
You can use $dateFromString operator to convert $DateC to ISODate,
Make sure your input should be in ISODate format,
db.MyCollection.find([
{
$expr: {
$lte: [
{
$dateFromString: {
dateString: "$DateC",
format: "%d/%m/%Y"
}
},
ISODate("2020-03-05T00:00:00.000Z")
]
}
}
])
Playground
I am not sure with your date format, I have predicted as d/m/y, you can manage it your self if its different.
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()}
}
}
I was trying to write "WHERE (CASE ... THEN ... ELSE ... END) > 0" to sequelize v3.33 but couldn't find the solution yet.
Had tried sequelize.literal('...') but didn't work.
Using "HAVING" is one of the solutions but it's no good for performance-wise for large data extraction and it's twice as slow.
This is just an example of MySQL code but pretty much close to what I want to achieve.
SELECT
(CASE `a`.`fee` IS NULL
THEN `a.b`.`fee`
ELSE `a`.`fee`
END) AS `_fee`
FROM `a`
WHERE
(CASE `a`.`fee` IS NULL
THEN `a.b`.`fee`
ELSE `a`.`fee`
END) > 0 AND
(created_at > currentDate
AND
created_at < futureDate)
I want to convert this to sequelize. Below is as far as I can go, I don't know how to add that case closure.
models.a.findAll({
...
where: {
created_at: { $gt: startDate, $lt: endDate }
}
})
*** Don't mind about created_at, it's just an example.
You can use sequelize.where and sequelize.literal for that :
where:{
$and : [
{ created_at: { $gt: startDate, $lt: endDate } },
sequelize.where(sequelize.literal('(CASE `a`.`fee` IS NULL THEN `a.b`.`fee` ELSE `a`.`fee` END)') , { $gt : 0 } )
]
}
Note : this might not work as alias a. of the table might be diff, you can debug and change as per your query
I'm trying to search for the existence of a nested element, as well as getting a timestamp greater than:
db.stats.find( { $and: [ { 'data.Statistics': {$exists: true} },{ timestamp: {$gte: 1} } ] }
From the docs I can't see where I'm going wrong, but I'm not getting anything back.
Just doing:
var query = {};
query["data.Statistics"] = {$exists: true}
works however.
The $and operator is not really necessary in this case as it can be implicitly used by just specifying a comma separated list of expressions thus you can re-write your query as:
db.stats.find({
"data.Statistics": { "$exists": true },
"timestamp": { "$gte": 1 }
});
If using a variable to create the query object using the square-bracket notation, you can approach it like
var query = {};
query["data.Statistics"] = { "$exists": true };
query["timestamp"] = { "$gte": 1 };
db.stats.find(query);