0

Let's say we have a mongodb collection that has elements containing an int attribute value like: {"MyCollectionAttribute": 12345}

How can I search the string "234" inside the int using Query<T>. syntax?

For now it seems to work(as explained here) using raw query like:

var query = new QueryDocument("$where", "/234/.test(this.MyCollectionAttribute)");
myCollection.Find(query);

Is it preferable to store the values directly as strings instead of integers, since a regex match will be slow? How do you approach theese situations?

Edit

Context: a company can have some internal codes that are numbers. In sql server they can be stored as a column of int type in order to have data integrity at database level and then queried from linq to sql with something like:

.where(item => item.CompanyCode.ToString().Contains("234"))

In this way there is both data integrity at db level and type safety of the query.

I asked the question in order to see how this scenario can be implemented using mongodb.

Community
  • 1
  • 1
eugen
  • 464
  • 5
  • 16
  • It's all about your requirements. If you're going to be doing this sort of query a lot then it probably makes sense to add a string version of the field, but if you're only occasionally doing it then that would be overkill. – JohnnyHK Sep 07 '12 at 15:02

1 Answers1

0

Does not make much sense what you are asking.

Regular expressions are for search within strings and not within integers.

If you want to perform a substring search (for whatever reason) then store your numbers as strings and not as integers - obviously.

  • I edited the question and explained the context. Saving numbers as strings is an option but it loses data integrity. – eugen Sep 08 '12 at 08:35