I have a 6000 records with a problem: test_date is saved wrongly; for example for a date in the 2nd month the data is mistakenly saved as the 4th month:
"test_date" : ISODate("2017-04-02T00:00:00.000+0000"),
I need to change month to 02 and date to 04; I need a query to run on mongochef to change these fields.
Use $set to update fields in mongodb,
db.collection.update({
"test_date": ISODate("2017-04-02T00:00:00.000+0000")
}, {
$set: {
"test_date": ISODate("2017-02-04T00:00:00.000+0000")
}
}, {
multi: true
});
Note: this command will set same date "YYYY-MM-DDThh:mm:ssTZD" for all matches
Related
How to convert a SQL query to mongodb query?
Please write a mongodb query - this is my query in SQL:
UPDATE user
SET expireIn = DATEADD(DAY, 2, expireIn)
WHERE phone = '123434574'
I want to add some day to expireIn column.
expireIn field is ISODate and also has a value for the time.
Welcome Mostafa Asadi,
You can do something like this:
db.collection.update({
phone: "123434574"
},
[
{
$set: {
"expireIn": {
$dateAdd: {
startDate: "$expireIn",
unit: "day",
amount: 2
}
}
}
}
],{multi:true})
As you can see on the playground.
The first {} are the matching part, which documents do you want to update. The second part is the updating, here inside [] as this is a pipeline, using the $dateAdd function.
Edit:
with {multi: true} for multiple documents update
I want to obtain the records that the "FECHA_FIN" field is greater than or equal to today's date.
this is an example of my data:
but with this query:
db.getCollection('susp_programadas').find( {"FECHA_FIN":{ $gte: new Date("YYYY-mm-dd") }} )
I do not get results, what am I doing wrong? Thank you
You can convert the date to an ISO date and query that way. Since you stored the date as a string mongo has no idea how to query it against an ISO date without conversion.
If you stored your date in mongo as the default ISO date then you could have easily done this:
db.getCollection('susp_programadas').find({"FECHA_FIN":{$gte: new Date()}})
So this is how you can do it now:
db.getCollection('susp_programadas').aggregate([
{
$project: {
date: { $dateFromString: { dateString: '$FECHA_FIN' }}
}
},
{ $match: { date: { $gte: new Date() }}}
])
You can use the $dateFromString in an aggregate query with a $match to get the results you want. Note that $dateFromString is only available in MongoDB version 3.6 and up.
If there is no way to convert your data to ISODate or upgrade your DB you could also consider another solution which via $where:
db.getCollection('susp_programadas').find({
$where: function(){
return new Date(this.FECHA_FIN) > Date.now()
}
})
However that solution suffers from the fact that $where can not use indexes so have that in mind.
I need a help on mongoid date range query.
I am trying to query events that spans over certain date range.
The event collection has two fields in the database start_date and end_date.
What would be the query to, for example, find events that occur between 04-18-2015 to 04-28-2015?
This might be helpful to you :
db.collectionName.find({
"start_date": { $lt: endDate }, // add end date here
"end_date": { $gt: startDate } // add start date here
});
Documents in my collection include this structure:
"time" : {
"start" : ISODate("2013-06-10T20:31:48.694Z"),
"end" : ISODate("2013-12-11T20:31:48.694Z")
}
Based off the current Time in which the query was made, I need Mongo to return which documents are currently "live" (in that the current time falls between the start and end time of each document).
The only mongo query I can find in this area queries for documents created between two Dates:
items.find({
created_at: {
$gte:"Mon May 30 18:47:00 +0000 2015",
$lt: "Sun May 30 20:40:36 +0000 2010"
}
})
What does this "live" query look like?
Like this:
var currentTime = new Date();
items.find({
'time.start': {$lt: currentTime},
'time.end': {$gt: currentTime}
});
Which will find the docs where the current time is between the start and end times in the doc.
I currently have a collection with documents like the following:
{ foo: 'bar', timeCreated: ISODate("2012-06-28T06:51:48.374Z") }
I would now like to add a timestampCreated key to the documents in this collection, to make querying by time easier.
I was able to add the new column with an update and $set operation, and set the timestamp value but I appears to be setting the current timestamp using this:
db.reports.update({}, {
$set : {
timestampCreated : new Timestamp(new Date('$.timeCreated'), 0)
}
}, false, true);
I however have not been able to figure out a way to add this column and set it's value to the timestamp of the existing 'timeCreated' field.
Do a find for all the documents, limiting to just the id and timeCreated fields. Then loop over that and generate the timestampCreated value, and do an update on each.
Use updateMany() which can accept aggregate pipelines (starting from MongoDB 4.2) and thus take advantage of the $toLong operator which converts a Date into the number of milliseconds since the epoch.
Also use the $type query in the update filter to limit only documents with the timeCreated field and of Date type:
db.reports.updateMany(
{ 'timeCreated': {
'$exists': true,
'$type': 9
} },
[
{ '$set': {
'timestampCreated': { '$toLong': '$timeCreated' }
} }
]
)