MongoDB search by DateTime type not working

好久不见. 提交于 2019-12-25 07:17:06

问题


Hi Im new to Mongo DB and trying to start with it, i created a collection and inserted this document into it

{
"_id" : ObjectId("574ad7c42368e58f8c07a47d"),
"id" : 10000,
"BookieId" : 1,
"EventBookieId" : null,
"LeagueId" : 214,
"LeagueNameId" : 4571,
"Team1Id" : 435,
"Team1NameId" : null,
"Team2Id" : 430,
"Team2NameId" : null,
"EventDateTime" : "2013-09-14T14:00:00",
"isNew" : true,
"CountryId" : 36,
"isCountry" : true,
"isLeague" : true,
"Gameid" : null,
"Reversed" : false,
"eventId" : 10000,
"bets" : [ 
    {
        "id" : 1,
        "EventConnectionId" : 10000,
        "isCorners" : false,
        "DateTime" : "2013-09-10T09:06:43.333",
        "IsLive" : false,
        "Team1Score" : null,
        "Team2Score" : null,
        "Team1RedCards" : null,
        "Team2RedCards" : null,
        "odds" : []
    }, 
    {
        "id" : 2,
        "EventConnectionId" : 10000,
        "isCorners" : false,
        "DateTime" : "2013-09-10T19:50:26.84",
        "IsLive" : false,
        "Team1Score" : 0,
        "Team2Score" : 0,
        "Team1RedCards" : null,
        "Team2RedCards" : null,
        "odds" : [ 
            {
                "id" : 62282718,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "21968173",
                "ScrapingDateTime" : "2013-09-13T08:39:15.81",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 1.81,
                "Odds2" : 2.1236,
                "Odds3" : null,
                "MaxBet" : 2000,
                "Bet" : "Hcap",
                "BetGamePart" : 0,
                "Line" : -0.25,
                "ID_IDENTITY" : 1
            }, 
            {
                "id" : 62282719,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "21968175",
                "ScrapingDateTime" : "2013-09-13T08:39:15.81",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.04167,
                "Odds2" : 1.86,
                "Odds3" : null,
                "MaxBet" : 2000,
                "Bet" : "Total",
                "BetGamePart" : 0,
                "Line" : 2.0,
                "ID_IDENTITY" : 2
            }, 
            {
                "id" : 62282720,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "21968177",
                "ScrapingDateTime" : "2013-09-13T08:39:15.81",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.16,
                "Odds2" : 3.45,
                "Odds3" : 3.05,
                "MaxBet" : 1000,
                "Bet" : "X12",
                "BetGamePart" : 0,
                "Line" : null,
                "ID_IDENTITY" : 3
            }, 
            {
                "id" : 62282721,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "21968179",
                "ScrapingDateTime" : "2013-09-13T08:39:15.81",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.21951,
                "Odds2" : 1.7,
                "Odds3" : null,
                "MaxBet" : 1000,
                "Bet" : "Hcap",
                "BetGamePart" : 1,
                "Line" : -0.25,
                "ID_IDENTITY" : 4
            }, 
            {
                "id" : 62282722,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "21968180",
                "ScrapingDateTime" : "2013-09-13T08:39:15.81",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.78,
                "Odds2" : 3.9,
                "Odds3" : 1.99,
                "MaxBet" : 500,
                "Bet" : "X12",
                "BetGamePart" : 1,
                "Line" : null,
                "ID_IDENTITY" : 5
            }, 
            {
                "id" : 62282723,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "21968181",
                "ScrapingDateTime" : "2013-09-13T08:39:15.81",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 1.93,
                "Odds2" : 1.95,
                "Odds3" : null,
                "MaxBet" : 1000,
                "Bet" : "Total",
                "BetGamePart" : 1,
                "Line" : 0.75,
                "ID_IDENTITY" : 6
            }, 
            {
                "id" : 62290838,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "14382209",
                "ScrapingDateTime" : "2013-09-13T15:38:11.303",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 1.81,
                "Odds2" : 2.1236,
                "Odds3" : null,
                "MaxBet" : 2000,
                "Bet" : "Hcap",
                "BetGamePart" : 0,
                "Line" : -0.25,
                "ID_IDENTITY" : 7
            }, 
            {
                "id" : 62290839,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "14382211",
                "ScrapingDateTime" : "2013-09-13T09:27:39.6",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.05263,
                "Odds2" : 1.85,
                "Odds3" : null,
                "MaxBet" : 2000,
                "Bet" : "Total",
                "BetGamePart" : 0,
                "Line" : 2.0,
                "ID_IDENTITY" : 8
            }, 
            {
                "id" : 62290840,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "14382213",
                "ScrapingDateTime" : "2013-09-13T10:29:22.863",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.15,
                "Odds2" : 3.45,
                "Odds3" : 3.1,
                "MaxBet" : 1000,
                "Bet" : "X12",
                "BetGamePart" : 0,
                "Line" : null,
                "ID_IDENTITY" : 9
            }, 
            {
                "id" : 62290841,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "14382215",
                "ScrapingDateTime" : "2013-09-13T15:38:11.303",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.21951,
                "Odds2" : 1.7,
                "Odds3" : null,
                "MaxBet" : 1000,
                "Bet" : "Hcap",
                "BetGamePart" : 1,
                "Line" : -0.25,
                "ID_IDENTITY" : 10
            }, 
            {
                "id" : 62290842,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "14382216",
                "ScrapingDateTime" : "2013-09-13T15:38:11.303",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.78,
                "Odds2" : 3.9,
                "Odds3" : 1.99,
                "MaxBet" : 500,
                "Bet" : "X12",
                "BetGamePart" : 1,
                "Line" : null,
                "ID_IDENTITY" : 11
            }, 
            {
                "id" : 62290843,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "14382217",
                "ScrapingDateTime" : "2013-09-13T15:38:11.303",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 1.93,
                "Odds2" : 1.95,
                "Odds3" : null,
                "MaxBet" : 1000,
                "Bet" : "Total",
                "BetGamePart" : 1,
                "Line" : 0.75,
                "ID_IDENTITY" : 12
            }, 
            {
                "id" : 62381139,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "20864356",
                "ScrapingDateTime" : "2013-09-13T15:38:11.303",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.05263,
                "Odds2" : 1.85,
                "Odds3" : null,
                "MaxBet" : 2000,
                "Bet" : "Total",
                "BetGamePart" : 0,
                "Line" : 2.0,
                "ID_IDENTITY" : 13
            }, 
            {
                "id" : 62381140,
                "BetId" : 2,
                "BookieId" : 1,
                "BookieBetId" : "20864358",
                "ScrapingDateTime" : "2013-09-13T15:38:11.303",
                "MinuteInGame" : null,
                "GamePart" : null,
                "Odds1" : 2.15,
                "Odds2" : 3.45,
                "Odds3" : 3.1,
                "MaxBet" : 1000,
                "Bet" : "X12",
                "BetGamePart" : 0,
                "Line" : null,
                "ID_IDENTITY" : 14
            }
        ]
    }
]

}

now I'm trying to find by date with this Query

db.EventsBetsOdds.find( { "bets": { $elemMatch: { "DataTime": "2013-09-10T09:06:43.333" } } } );

Fetched 0 record(s) in 400ms is what i get, what am i doing wrong?

i tried to put an index on this field, indexing finished


回答1:


Your mongo query should be like this:

 db.testes.find({
     'bets.DateTime': '2013-09-10T09:06:43.333'
 }, {
     'bets': {
         $elemMatch: {
             'DateTime': '2013-09-10T09:06:43.333'
         }
     }
 })

Or something like this:

db.testes.find({
    'bets.DateTime': '2013-09-10T09:06:43.333'
}, {
    'bets.$': 1
})

Helpful links:

  • $ (projection operator)
  • $elemMatch - Array of Embedded Documents
  • Array Field Limitations
  • $filter (aggregation)


来源:https://stackoverflow.com/questions/37510908/mongodb-search-by-datetime-type-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!