Mongodb query does not use prefix on compound index with text field

后端 未结 2 1521
刺人心
刺人心 2021-01-14 06:48

I\'ve created the following index on my collection:

db.myCollection.createIndex({
  user_id: 1,
  name: \'text\'
})

If I try to see the exe

2条回答
  •  无人共我
    2021-01-14 07:28

    In general MongoDB can use index prefixes to support queries, however compound indexes including geospatial or text fields are a special case of sparse compound indexes. If a document does not include a value for any of the text index field(s) in a compound index, it will not be included in the index.

    In order to ensure correct results for a prefix search, an alternative query plan will be chosen over the sparse compound index:

    If a sparse index would result in an incomplete result set for queries and sort operations, MongoDB will not use that index unless a hint() explicitly specifies the index.

    Setting up some test data in MongoDB 3.4.5 to demonstrate the potential problem:

    db.myCollection.createIndex({ user_id:1, name: 'text' }, { name: 'myIndex'})
    
    // `name` is a string; this document will be included in a text index
    db.myCollection.insert({ user_id:123, name:'Banana' })
    
    // `name` is a number; this document will NOT be included in a text index
    db.myCollection.insert({ user_id:123, name: 456 })
    
    // `name` is missing; this document will NOT be included in a text index
    db.myCollection.insert({ user_id:123 })
    

    Then, forcing the compound text index to be used:

    db.myCollection.find({user_id:123}).hint('myIndex')
    

    The result only includes the single document with the indexed text field name, rather than the three documents that would be expected:

    {
      "_id": ObjectId("595ab19e799060aee88cb035"),
      "user_id": 123,
      "name": "Banana"
    }
    

    This exception should be more clearly highlighted in the MongoDB documentation; watch/upvote DOCS-10322 in the MongoDB issue tracker for updates.

提交回复
热议问题