MongoDB C# Text Search Returns 0 Results

1.2k views Asked by At

I am using the new 2.0 C# Driver for MongoDB. I am trying to create an autocomplete search box on my website.

I have a simple collection that has ID, Symbol, and Name:

/* 0 */ { "_id" : ObjectId("55743a1dbc8d1c60942ac524"), "Symbol" : "A", "Name" : "Agilent Technologies, Inc. Comm" }

/* 1 */ { "_id" : ObjectId("55743a2dbc8d1c60942ac525"), "Symbol" : "AA", "Name" : "Alcoa Inc. Common Stock" }

/* 2 */ { "_id" : ObjectId("55743a2dbc8d1c60942ac526"), "Symbol" : "AAC", "Name" : "AAC Holdings, Inc. Common Stock" }

/* 3 */ { "_id" : ObjectId("55743a2dbc8d1c60942ac527"), "Symbol" : "AAL", "Name" : "American Airlines Group, Inc." }

I created a text index using this command:

db.StockLookups.createIndex({Symbol: "text", Name: "text"})

I have the following method in my code:

public async Task<List<StockLookup>> SearchAsync(string searchString)
{
    var filter = Builders<StockLookup>.Filter.Text(searchString);

    return await _collection.Find(filter)
        .ToListAsync();
}

However, it is not returning the desired results. If I type in "AAL" I get back American Airlines which is what I want.

However, if I type in "AA" I only get back Alcoa. In this case I would want it to return Alcoa, AAC Holdings, and American Airlines.

What am I doing wrong and how can I correct the problem?

0

There are 0 answers