MongoDB查询用于获取'n'天前创建的记录

I have a MySQL query which I use run to fetch records created 27 days ago.

SELECT *
FROM appointments
WHERE DATE_FORMAT(created_dt, '%m/%d/%Y') = DATE_FORMAT(DATE_SUB(SYSDATE(), INTERVAL 27 DAY), '%m/%d/%Y') 

How do I do this in MongoDB?

I am using codeigniter, so +1 for a mongo_db query.

Depending on what you mean with "created 'n' days ago"

You can choose between one of the Comparison Query Operators

So if you want all records created exact ($eq) n days ago it would look something like this.

db.appointments.find({"created_dt" : { $eq : new ISODate("2016-05-04T12:00:00Z") }});

And you could calculate the Date from the current Date.

Note:

I don't know if you can use $eq and just pass a day and get all records equal to that day. It could work but you would need to try this first.

If it doesn't work you can make a workaround like this

db.appointments.find({"created_dt" : { $gte : today, $lt : nextDay}});

where today is today at 00:00:00 and nextDay is nextDay at 00:00:00 (note the $lt instead of $lte)