I have a collection that contains documents in the following format.
{
"_id" : ObjectId("58d9fa5cb35630097b96acab"),
"invoice_id" : ObjectId("58d9fa5cb35630097b96acaa"),
"amount" : 717.6,
"date" : "2017-03-28",
"by_id" : ObjectId("58d9d6a0b35630097b96a729")
},
{
"_id" : ObjectId("58d9ff77b35630097b96acb8"),
"invoice_id" : ObjectId("58d9ff77b35630097b96acb7"),
"amount" : 717.6,
"date" : "2017-03-28",
"by_id" : ObjectId("58d9d6a0b35630097b96a729")
}
I need to make a date comparison using aggregation, but dates in the documents are not in the correct format. So I'm trying to add a new field called c_date with correctly formatted date value, using $addFields function for each document.
db.table.aggregate([
{ $addFields: { c_date : new ISODate('2017-03-29')} }
])
The above query results
{
"_id" : ObjectId("58d9fa5cb35630097b96acab"),
"invoice_id" : ObjectId("58d9fa5cb35630097b96acaa"),
"amount" : 717.6,
"date" : "2017-03-28",
"by_id" : ObjectId("58d9d6a0b35630097b96a729"),
"c_date": 2017-03-29 00:00:00.000Z
},
{
"_id" : ObjectId("58d9ff77b35630097b96acb8"),
"invoice_id" : ObjectId("58d9ff77b35630097b96acb7"),
"amount" : 717.6,
"date" : "2017-03-28",
"by_id" : ObjectId("58d9d6a0b35630097b96a729")
"c_date": 2017-03-29 00:00:00.000Z
}
I want to add the formatted value of date field to the c_date field in the each document. How to achieve it?
"2017-03-28">"2017-03-01"istrue). - dgiuggforEach. - dgiugg