MongoDB在嵌套的文档数组中没有使用索引

时间:2016-08-31 04:46:37

标签: arrays mongodb indexing collections nested

我有一个大而复杂的集合,如下所示:

{ 
"_id" : ObjectId("574c55312368e5aa90bbf27b"), 
"id" : NumberInt(10000), 
"BookieId" : NumberInt(1), 
"EventBookieId" : null, 
"LeagueId" : NumberInt(214), 
"LeagueNameId" : NumberInt(4571), 
"Team1Id" : NumberInt(435), 
"Team1NameId" : null, 
"Team2Id" : NumberInt(430), 
"Team2NameId" : null, 
"EventDateTime" : ISODate("2013-09-14T13:00:00.000+0000"), 
"isNew" : true, 
"CountryId" : NumberInt(36), 
"isCountry" : true, 
"isLeague" : true, 
"Gameid" : null, 
"Reversed" : false, 
"eventId" : NumberInt(10000), 
"bets" : [
    {
        "id" : NumberInt(1), 
        "EventConnectionId" : NumberInt(10000), 
        "isCorners" : false, 
        "DateTime" : ISODate("2013-09-10T08:06:43.333+0000"), 
        "IsLive" : false, 
        "Team1Score" : null, 
        "Team2Score" : null, 
        "Team1RedCards" : null, 
        "Team2RedCards" : null, 
        "odds" : [

        ]
    }, 
    {
        "id" : NumberInt(2), 
        "EventConnectionId" : NumberInt(10000), 
        "isCorners" : false, 
        "DateTime" : ISODate("2013-09-10T18:50:26.840+0000"), 
        "IsLive" : false, 
        "Team1Score" : NumberInt(0), 
        "Team2Score" : NumberInt(0), 
        "Team1RedCards" : null, 
        "Team2RedCards" : null, 
        "odds" : [
            {
                "id" : NumberInt(62282718), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "21968173", 
                "ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 1.81, 
                "Odds2" : 2.1236, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(2000), 
                "Bet" : "Hcap", 
                "BetGamePart" : NumberInt(0), 
                "Line" : -0.25, 
                "ID_IDENTITY" : NumberInt(1)
            }, 
            {
                "id" : NumberInt(62282719), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "21968175", 
                "ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.04167, 
                "Odds2" : 1.86, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(2000), 
                "Bet" : "Total", 
                "BetGamePart" : NumberInt(0), 
                "Line" : 2.0, 
                "ID_IDENTITY" : NumberInt(2)
            }, 
            {
                "id" : NumberInt(62282720), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "21968177", 
                "ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.16, 
                "Odds2" : 3.45, 
                "Odds3" : 3.05, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "X12", 
                "BetGamePart" : NumberInt(0), 
                "Line" : null, 
                "ID_IDENTITY" : NumberInt(3)
            }, 
            {
                "id" : NumberInt(62282721), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "21968179", 
                "ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.21951, 
                "Odds2" : 1.7, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "Hcap", 
                "BetGamePart" : NumberInt(1), 
                "Line" : -0.25, 
                "ID_IDENTITY" : NumberInt(4)
            }, 
            {
                "id" : NumberInt(62282722), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "21968180", 
                "ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.78, 
                "Odds2" : 3.9, 
                "Odds3" : 1.99, 
                "MaxBet" : NumberInt(500), 
                "Bet" : "X12", 
                "BetGamePart" : NumberInt(1), 
                "Line" : null, 
                "ID_IDENTITY" : NumberInt(5)
            }, 
            {
                "id" : NumberInt(62282723), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "21968181", 
                "ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 1.93, 
                "Odds2" : 1.95, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "Total", 
                "BetGamePart" : NumberInt(1), 
                "Line" : 0.75, 
                "ID_IDENTITY" : NumberInt(6)
            }, 
            {
                "id" : NumberInt(62290838), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "14382209", 
                "ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 1.81, 
                "Odds2" : 2.1236, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(2000), 
                "Bet" : "Hcap", 
                "BetGamePart" : NumberInt(0), 
                "Line" : -0.25, 
                "ID_IDENTITY" : NumberInt(7)
            }, 
            {
                "id" : NumberInt(62290839), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "14382211", 
                "ScrapingDateTime" : ISODate("2013-09-13T08:27:39.600+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.05263, 
                "Odds2" : 1.85, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(2000), 
                "Bet" : "Total", 
                "BetGamePart" : NumberInt(0), 
                "Line" : 2.0, 
                "ID_IDENTITY" : NumberInt(8)
            }, 
            {
                "id" : NumberInt(62290840), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "14382213", 
                "ScrapingDateTime" : ISODate("2013-09-13T09:29:22.863+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.15, 
                "Odds2" : 3.45, 
                "Odds3" : 3.1, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "X12", 
                "BetGamePart" : NumberInt(0), 
                "Line" : null, 
                "ID_IDENTITY" : NumberInt(9)
            }, 
            {
                "id" : NumberInt(62290841), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "14382215", 
                "ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.21951, 
                "Odds2" : 1.7, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "Hcap", 
                "BetGamePart" : NumberInt(1), 
                "Line" : -0.25, 
                "ID_IDENTITY" : NumberInt(10)
            }, 
            {
                "id" : NumberInt(62290842), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "14382216", 
                "ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.78, 
                "Odds2" : 3.9, 
                "Odds3" : 1.99, 
                "MaxBet" : NumberInt(500), 
                "Bet" : "X12", 
                "BetGamePart" : NumberInt(1), 
                "Line" : null, 
                "ID_IDENTITY" : NumberInt(11)
            }, 
            {
                "id" : NumberInt(62290843), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "14382217", 
                "ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 1.93, 
                "Odds2" : 1.95, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "Total", 
                "BetGamePart" : NumberInt(1), 
                "Line" : 0.75, 
                "ID_IDENTITY" : NumberInt(12)
            }, 
            {
                "id" : NumberInt(62381139), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "20864356", 
                "ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.05263, 
                "Odds2" : 1.85, 
                "Odds3" : null, 
                "MaxBet" : NumberInt(2000), 
                "Bet" : "Total", 
                "BetGamePart" : NumberInt(0), 
                "Line" : 2.0, 
                "ID_IDENTITY" : NumberInt(13)
            }, 
            {
                "id" : NumberInt(62381140), 
                "BetId" : NumberInt(2), 
                "BookieId" : NumberInt(1), 
                "BookieBetId" : "20864358", 
                "ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"), 
                "MinuteInGame" : null, 
                "GamePart" : null, 
                "Odds1" : 2.15, 
                "Odds2" : 3.45, 
                "Odds3" : 3.1, 
                "MaxBet" : NumberInt(1000), 
                "Bet" : "X12", 
                "BetGamePart" : NumberInt(0), 
                "Line" : null, 
                "ID_IDENTITY" : NumberInt(14)
            }
        ]
    }
]

}

我在赔率数组中创建了一个 ScrapingDateTime 的索引,该数组又位于 bets 数组中。 这是索引:

{"bets.0.odds.0.ScrapingDateTime" : 1}

当我运行此查询时,它不使用索引并执行集合扫描,任何人都可以解释方式,或者我应该使用此索引做什么?

         db.EventsBetsOdds.find({'bets.odds.ScrapingDateTime':{'$gte':ISODate("2014-08-13T14:38:11.303Z"),
     '$lt':ISODate("2014-09-14T14:38:11.303Z")}},
     {"bets.odds.ScrapingDateTime":1,"bets.odds.Line":1,"bets.IsLive":1,_id:0}).explain()

1 个答案:

答案 0 :(得分:1)

索引{" bets.0.odds.0.ScrapingDateTime" :1}指的是名为" 0"的子文档,而不是位置零的数组元素。例如:

{
 "bets" : {
   "0" : {
     "odds" : {
       "0" : {
         "ScrapingDateTime" : .... 
       }
     }
   }
 }
}

所需的索引将是:

db.EventsBetsOdds.ensureIndex({ "bets.odds.ScrapingDateTime" : 1 })

如果您希望查询的索引为cover,则索引为:

db.EventsBetsOdds.createIndex({ 
 "bets.odds.ScrapingDateTime" : 1,
 "bets.odds.Line" : 1,
 "bets.IsLive" : 1 
})