[SERVER-35045] Unique Index on a single text field has unexpected behavior with spaces Created: 17/May/18  Updated: 27/Oct/23  Resolved: 23/May/18

Status: Closed
Project: Core Server
Component/s: Text Search
Affects Version/s: 3.6.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Mathew Cabutage [X] Assignee: Backlog - Triage Team
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Server Triage
Operating System: ALL
Steps To Reproduce:

Used the mongo shell on powershell.

> use test
switched to db test
> db.testCol.createIndex( { "name": "text" } , { unique: true } );
{
 "createdCollectionAutomatically" : true,
 "numIndexesBefore" : 1,
 "numIndexesAfter" : 2,
 "ok" : 1
}
> db.testCol.insert( {"name": "test co"} )
WriteResult( { "nInserted" : 1 })
> db.testCol.insert( {"name": "test2 co"})
WriteResult({
 "nInserted" : 0,
 "writeError" : {
 "code" : 11000,
 "errmsg" : "E11000 duplicate key error collection: test.testCol index: name_text dup key: { : \"co\", : 0.75 }"
 }
})

Participants:

 Description   

Applying a unique index to a single text field has unexpected behavior with spaces.

OS: Windows 10

For example:

Inserting "test co" and "test2 co" fail uniqueness.

Inserting " co" and "       co" fail uniqueness.

 



 Comments   
Comment by Mathew Cabutage [X] [ 21/May/18 ]

Thanks for the help Andy. You are right, this is not a bug, but user error. I should be just be indexing strings.

As far as it possibly being a bug that mongodb allows the creation of a unique text index, I do see it possibly being useful when used with weights.

You may close/delete the issue.

Comment by Andy Schwerin [ 18/May/18 ]

It may be a bug that we allow you to create a unique text index. A text index splits strings into tokens (words), and those tokens form the keys. So in your example, "co" is a duplicates key.

Are you trying to build a text index, or just index strings? If the latter, you need to change your index spec.

Generated at Thu Feb 08 04:38:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.