从现在开始在MongoDB中查找7天前的记录

从现在开始在MongoDB中查找7天前的记录

本文介绍了从现在开始在MongoDB中查找7天前的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合,其中包含的对象表示如下:

I've a collection containing objects represented like the one below:

/* 1 */
{
    "_id" : ObjectId("551c6605e4c6ac495c923aab"),
    "sender_id" : ObjectId("551c6605e4c6ac495c923aac"),
    "rep_sender_id" : "38",
    "sender_name" : "Melissa Aldridge",
    "sender_title" : null,
    "sender_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/qlv_aldridge_melissa.jpg",
    "sender_company" : "",
    "recipient_id" : ObjectId("5356af6ae4c6acdc10450a23"),
    "recipient_name" : "MAX GUCILATAR",
    "recipient_title" : "",
    "recipient_photo_id" : "webdrProfileIcon.png",
    "message" : [
        {
            "message_link" : "20150401174059801537155",
            "message_href" : "//url2/pdone/pharma_brandpage?id=20150401174059801537155&brand_id=11",
            "message_href_text" : "Co-pay Card Savings Information"
        }
    ],
    "message_subject" : "has shared information about ADHD treatment option with you",
    "sender_type" : "rep",
    "message_type" : "share",
    "supplemental" : "",
    "reply_ok" : "no",
    "mdate" : ISODate("2015-04-01T21:41:25.523Z"),
    "date" : "Wed April 1, 2015",
    "read" : "no",
    "timeago" : "2015-04-01T17:41:25-04:00",
    "reference_count" : 1,
    "chain" : [],
    "rep_header" : {
        "paccode" : "PFIZER_EMAIL_SEG1",
        "sync" : "",
        "_st" : "ejR2NGQ0eDVrNWw1ajVqNXc0ZjN2NG4zNDQ0NDAzeDU2NDY0dTVsMjQ0YTVhMms1NjNuNDIzbDJzNG00YTU2MzU0NTNtNW0yMjQyNDIzbjJhMzA1YjNiNHQ1NTVkNGE0cDNsNHozdzIwM2o0",
        "hcp_email_address" : "[email protected]",
        "email_target" : "tmb",
        "inactive" : "no",
        "share_date" : "4-1-2015",
        "share_date_2" : ISODate("2015-04-01T09:41:25.000Z"),
        "hcp_id" : "135787",
        "rep_msg" : "Thank you for taking time to speak with me. Based on our discussion, I’ve attached additional materials about Quillivant XR™ (methylphenidate HCI) CII. I look forward to following up with you in the near future. Thank you.",
        "session_id" : "2015040117405239775517",
        "rep_id" : "38",
        "hcp_fname" : "MAX",
        "hcp_lname" : "GUCILATAR",
        "address" : "51 OFFICE PARK DRIVE",
        "city" : "JACKSONVILLE",
        "zip" : "28546",
        "program_path" : "https://url/web/reptool/set/",
        "rep_fname" : "Melissa",
        "rep_lname" : "Aldridge",
        "rep_title" : null,
        "rep_alias" : "Aldridge,Melissa",
        "rep_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/qlv_aldridge_melissa.jpg",
        "email_subject" : "Melissa Aldridge has shared information about an ADHD treatment option with you.",
        "mongo_id" : "5356af6ae4c6acdc10450a23"
    },
    "rep_shares" : [
        {
            "media_show_citation" : "no",
            "server_path_to_media" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/cost_cov-1.pdf",
            "company_id" : 6,
            "media_citation" : "",
            "media_id" : 47,
            "media_size" : "10",
            "media_title" : "Co-pay%20Card%20Savings%20Information",
            "media_description" : "Learn%20how%20eligible%20patients%20can%20save%20with%20the%20Quillivant%20XR%E2%84%A2%20%28methylphenidate%20HCl%29%20CII%20savings%20program.",
            "media_type" : "pdf",
            "media_icon" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/cost_cov-1-icon.jpg",
            "session_presentation_id" : "20150401174059801537155",
            "media_units" : "Pages",
            "brand_id" : 11,
            "media_expiration_timestamp" : 457393259.2305399800000000
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("551c2b0ee4c6ac495c923a97"),
    "sender_id" : ObjectId("551c2b0ee4c6ac495c923a98"),
    "rep_sender_id" : "33",
    "sender_name" : "Pamela Washington",
    "sender_title" : null,
    "sender_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/Pamela_Washington.jpg",
    "sender_company" : "",
    "recipient_id" : ObjectId("53909cb7e4c6acaa18486fb2"),
    "recipient_name" : "ALAN WEINTRAUB",
    "recipient_title" : "",
    "recipient_photo_id" : "webdrProfileIcon.png",
    "message" : [
        {
            "message_link" : "201504011329211223504837",
            "message_href" : "//url2/pdone/pharma_brandpage?id=201504011329211223504837&brand_id=11",
            "message_href_text" : "Efficacy and Safety Information"
        }
    ],
    "message_subject" : "has shared information about ADHD treatment option with you",
    "sender_type" : "rep",
    "message_type" : "share",
    "supplemental" : "",
    "reply_ok" : "no",
    "mdate" : ISODate("2015-04-01T17:29:50.718Z"),
    "date" : "Wed April 1, 2015",
    "read" : "no",
    "timeago" : "2015-04-01T13:29:50-04:00",
    "reference_count" : 1,
    "chain" : [],
    "rep_header" : {
        "paccode" : "PFIZER_EMAIL_SEG1",
        "sync" : "",
        "_st" : "ejR2NGQ0eDVrNWw1ajVqNXc0ZjN2NG4zNDQ0NDAzeDU2NDY0dTVsMjQ0YTVhMms1NjNuNDIzbDJzNG00YTU2MzU0NTNtNW0yMjQyNDIzbjJhMzA1YjNiNHQ1NTVkNGE0cDNsNHozdzIwM2o0",
        "hcp_email_address" : "[email protected]",
        "email_target" : "tmb",
        "inactive" : "no",
        "share_date" : "4-1-2015",
        "share_date_2" : ISODate("2015-04-01T05:29:50.000Z"),
        "hcp_id" : "141274",
        "rep_msg" : "Thank you for taking time to speak with me. Based on our discussion, I’ve attached additional materials about Quillivant XR™ (methylphenidate HCI) CII. I look forward to following up with you in the near future. Thank you.",
        "session_id" : "20150401132911346416403",
        "rep_id" : "33",
        "hcp_fname" : "ALAN",
        "hcp_lname" : "WEINTRAUB",
        "address" : "BEHAVIORAL ASSO., P.C.,STE 120",
        "city" : "NORCROSS",
        "zip" : "30092",
        "program_path" : "https://url/web/reptool/set/",
        "rep_fname" : "Pamela",
        "rep_lname" : "Washington",
        "rep_title" : null,
        "rep_alias" : "Washington,Pamela C",
        "rep_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/Pamela_Washington.jpg",
        "email_subject" : "Pamela Washington has shared information about an ADHD treatment option with you.",
        "mongo_id" : "53909cb7e4c6acaa18486fb2"
    },
    "rep_shares" : [
        {
            "media_show_citation" : "no",
            "server_path_to_media" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/eff_ramp.pdf",
            "company_id" : 6,
            "media_citation" : "",
            "media_id" : 46,
            "media_size" : "10",
            "media_title" : "Efficacy%20and%20Safety%20Information",
            "media_description" : "An%20overview%20of%20the%20efficacy%20and%20Important%20Safety%20Information%20for%20Quillivant%20XR%E2%84%A2%20%28methylphenidate%20HCl%29%20CII%2C%20including%20clinical%20data%20that%20show%20improvement%20in%20just%2045%20minutes%2C%20with%20efficacy%20at%204%20hours%20%28primary%20endpoint%29%20that%20lasts%20to%2012%20hours%20post-dosing.",
            "media_type" : "pdf",
            "media_icon" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/eff_ramp-icon.jpg",
            "session_presentation_id" : "201504011329211223504837",
            "media_units" : "Pages",
            "brand_id" : 11,
            "media_expiration_timestamp" : 457378161.7262100000000000
        }
    ]
}

我需要根据 rep_header.share_date rep_header.share_date_2 查找最近7天的记录(文档).我正在使用此代码,但不起作用:

I need to find the records (documents) from the last 7 days based on rep_header.share_date or by rep_header.share_date_2. I'm playing with this code but doesn't work:

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

由于我没有结果(获取0条记录),因此您可以看到示例文档来自 01/04 ,这是怎么回事?

Since I get no results (Fetched 0 record(s)) and as you can see the example document is from 01/04 what is wrong?

推荐答案

这是怎么回事:

db.messages.find({ "rep_header.share_date_2": { '$gte': lastWeek }} )

这篇关于从现在开始在MongoDB中查找7天前的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:19