0

"address":"570 Waldane Court, Salix, California, 4869","about" How i can find all users who lived in California? Uses $in operator?

tranntus
  • 61
  • 1
  • 8

1 Answers1

0

It would be easier to use regex search instead of an $in operator . Following would an example of regex search for the string California in the address attribute.

db.<collection>.find({""address":{"$regex":/California/i}},{"_id":1})

The i is being used for case insensitive search. If there is no text index on the address attribute the searching could take long assuming there are huge number of documents in the collection.

Although,$in can also be used it would be slightly complex query , using the aggregation framework.

db.<collection>.aggregate([
{ $project : { "_id":1, address_token : { $split: ["$address", " "] } } },
{ $unwind : "$address_token" },
{ $match : { address_token : /california/i } },
{ $group : { _id: "$_id" } }
]);

The first stage splits the address attribute in tokens with delimiter as whitespace.Following which the array of tokens is transformed to one row of each array element , searching for the key word california. Finally, grouping on the _id attribute incase the regex pattern is repeated in the address attribute.

mintekhab
  • 203
  • 1
  • 3