Question

Using mongodb with the NoRM driver I have this document:

{
    "_id" : ObjectId("0758030341b870c019591900"),
    "TmsId" : "EP000015560091",
    "RootId" : "1094362",
    "ConnectorId" : "SH000015560000",
    "SeasonId" : "7894681",
    "SeriesId" : "184298",
    "Titles" : [
            {
                    "Size" : 120,
                    "Type" : "full",
                    "Lang" : "en",
                    "Description" : "House"
            },
            {
                    "Size" : 10,
                    "Type" : "red",
                    "Lang" : "en",
                    "Description" : "House M.D."
            }
    ], yadda yadda yadda

and I am querying like:

var query = new Expando();
query["Titles.Description"] = Q.In(showNames);
var fuzzyMatches = db.GetCollection<Program>("program").Find(query).ToList();

where showNames is a string[] contain something like {"House", "Glee", "30 Rock"}

My results contain fuzzy matches. For example the term "House" returns every show with a Title with the word House in it ( like its doing a Contains ).

What I would like is straight matches. So if document.Titles contains "A big blue House" it does not return a match. Only if Titles.Description contains "House" would I like a match.

Was it helpful?

Solution

I haven't been able to reproduce the problem, perhaps because we're using different versions of MongoDB and/or NoRM. However, here are some steps that may help you to find the origin of the fuzzy results.

  1. Turn on profiling, using the MongoDB shell:

    > db.setProfilingLevel(2)
    
  2. Run your code again.
  3. Set the profiling level back to 0.
  4. Review the queries that were executed:

    > db.system.profile.find()
    

The profiling information should look something like this:

{
  "ts" : "Wed Dec 08 2010 09:13:13 GMT+0100",
  "info" : "query test.program ntoreturn:2147483647 reslen:175 nscanned:3  \nquery: { query: { Titles.Description: { $in: [ \"House\", \"Glee\", \"30 Rock\" ] } } }  nreturned:1 bytes:159",
  "millis" : 0
}

The actual query is in the info property and should be:

{ Titles.Description: { $in: [ "House", "Glee", "30 Rock" ] } }

If your query looks different, then the 'problem' is in the NoRM driver. For example, if NoRM translates your code to the following regex query, it will do a substring match:

{ Titles.Description: { $in: [ /House/, /Glee/, /30 Rock/ ] } }

I have used NoRM myself, but I haven't come across a setting to control this. Perhaps you're using a different version, that does come with such functionality.

If your query isn't different from what it should by, try running the query from the shell. If it still comes up with fuzzy results, then we're definitely using different versions of MongoDB ;)

OTHER TIPS

in shell syntax:

db.mycollection.find( { "Titles.Description" : "House" } )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top