Mongodb – Query MongoDB collection and find rows from 7 days ago

mongodbnosql

I have a MongoDB collection called messages and this is an example document for that collection:

{
    "_id": {
        "$oid": "551943eee4c6acf40fe5a0f2"
    },
    "sender_id": {
        "$oid": "551943eee4c6acf40fe5a0f3"
    },
    "rep_sender_id": "120",
    "sender_name": "Karen Morris",
    "sender_title": null,
    "sender_photo_id": "https://reptool_rpc.pdone.com/web/bundles/pdone/media/representative/photo/qlv_morris_karen.jpg",
    "sender_company": "",
    "recipient_id": {
        "$oid": "5367e006e4c6ac5b0d1b8e29"
    },
    "recipient_name": "KARIS KNIGHT",
    "recipient_title": "",
    "recipient_photo_id": "webdrProfileIcon.png",
    "message": [
        {
            "message_link": "20150330073847782685415",
            "message_href": "//www.themedicalbag.com/pdone/pharma_brandpage?id=20150330073847782685415&brand_id=11",
            "message_href_text": "Prescribing "
        }
    ],
    "message_subject": "has shared information about ADHD treatment option with you",
    "sender_type": "rep",
    "message_type": "share",
    "supplemental": "",
    "reply_ok": "no",
    "mdate": {
        "$date": "2015-03-30T12:39:10.170Z"
    },
    "date": "Mon March 30, 2015",
    "read": "no",
    "timeago": "2015-03-30T08:39:10-04:00",
    "reference_count": 1,
    "chain": [],
    "rep_header": {
        "paccode": "PFIZER_EMAIL_SEG1",
        "sync": "",
        "_st": "ejR2NGQ0eDVrNWw1ajVqNXc0ZjN2NG4zNDQ0NDAzeDU2NDY0dTVsMjQ0YTVhMms1NjNuNDIzbDJzNG00YTU2MzU0NTNtNW0yMjQyNDIzbjJhMzA1YjNiNHQ1NTVkNGE0cDNsNHozdzIwM2o0",
        "hcp_email_address": "karis@magnoliafamily.com",
        "email_target": "tmb",
        "inactive": "no",
        "share_date": "3-30-2015",
        "share_date_2": {
            "$date": "2015-03-30T12:39:10.000Z"
        },
        "hcp_id": "109270",
        "rep_msg": "Thank you for taking time to speak with me today. Here are some additional materials about Quillivant XR™ (methylphenidate HCI) CII. I look forward to following up with you in the near future. Thank you.",
        "session_id": "201503300738361681657321",
        "rep_id": "120",
        "hcp_fname": "KARIS",
        "hcp_lname": "KNIGHT",
        "address": "402 JOHNSTON STREET SE",
        "city": "DECATUR",
        "zip": "35601",
        "program_path": "https://reptool_rpc.pdone.com/web/reptool/set/",
        "rep_fname": "Karen",
        "rep_lname": "Morris",
        "rep_title": null,
        "rep_alias": "Morris,Karen",
        "rep_photo_id": "https://reptool_rpc.pdone.com/web/bundles/pdone/media/representative/photo/qlv_morris_karen.jpg",
        "email_subject": "Karen Morris has shared information about an ADHD treatment option with you.",
        "mongo_id": "5367e006e4c6ac5b0d1b8e29"
    },
    "rep_shares": [
        {
            "media_show_citation": "no",
            "server_path_to_media": "https://reptool_rpc.pdone.com/web/bundles/pdone/media/qlv_veeva/brand/presentation/2014_Quillivant_Downloadable_Letter.pdf",
            "company_id": 6,
            "media_citation": "",
            "media_id": 246,
            "media_size": "11",
            "media_title": "Prescribing%20Quillivant%20XR%E2%84%A2",
            "media_description": "Learn%20how%20to%20individualize%20the%20dose%20of%20Quillivant%20XR%E2%84%A2%20%28methylphenidate%20HCI%29%20CII%20according%20to%20the%20needs%20and%20response%20of%20the%20patient.",
            "media_type": "pdf",
            "media_icon": "https://reptool_rpc.pdone.com/web/bundles/pdone/media/qlv_veeva/brand/presentation/2014_Quillivant_Downloadable_Letter-icon.jpg",
            "session_presentation_id": "20150330073847782685415",
            "media_units": "Pages",
            "brand_id": 11,
            "media_expiration_timestamp": 457187927.50113
        }
    ]
}

I've read oficial docs and also this and this post here at SO looking for something helpful but can't find the proper docs for query the collection and get documents from 7 days ago only by using share_date_2. Can any give me some advice here?

Best Answer

I can't test this at the moment, but that's the basic format. You should change your date data type to ISO so it works easily with the below code:

var lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() -7);
db.messages.find({ "rep_header.share_date_2.$date": { $gte: lastWeek} } } );