jimbo2087
jimbo2087

Reputation: 1054

Mongodb Text Search and ID field

I want to search multiple fields in a collection for a phrase/string. I also need to filter the results to a single ID field.

For example, search the users collection for "john"

db.users.runCommand( "text", { search: "john" } )

When the document looks like:

{
    "_id" : ObjectId("525d5f3fa385ab082e8b4693"),
    "first_name" : "John",
    "last_name" : "Doe",
    "email" : "[email protected]",
    "account_id" : 1,
    "updated_at" : ISODate("2013-10-15T15:29:03.951Z"),
    "created_at" : ISODate("2013-10-15T15:29:03.951Z")
}

What's the best way to also filter the results to a specific ID, in the case above "account_id", so I'd only be searching/returning results where "john" appears and the account_id equals "1" for example?

Upvotes: 2

Views: 1784

Answers (1)

jimbo2087
jimbo2087

Reputation: 1054

I believe this is done using the filter option:

db.users.runCommand( "text", {
                                   search: "john",
                                   filter: { account_id : 1 }
                                 }
                       )

Or from the docs - http://docs.mongodb.org/manual/tutorial/limit-number-of-items-scanned-for-text-search/

Upvotes: 3

Related Questions