0

I'm using Symfony and Mongo. I have documents for each of my mongo collections. In the documents I defined all fields to be strings, even though some of them typically contain numerical values.

I perform searches in these collections and filter them like this:

private function like($paramValue) {
    return new \MongoRegex("/.*" . FilterHelper::castValue(FilterType::INT, $paramValue) . ".*/ix");
}
...
$qb->field('articleId')->equals( $this->like($params['articleCode']) );

However, values are persisted as numbers in mongo, "article_id": NumberInt(197568). The document is written like this:

/**
 * @MongoDB\String(name="article_id")
 */
protected $articleId; 

Naturally, queries fail because the type is not the expected. How can I fix this? I can't assume $articleId is a number.

ecc
  • 1,490
  • 1
  • 17
  • 42

1 Answers1

0

Did you thought about strval php function?

$qb->field('articleId')->equals( $this->like(strval($params['articleCode'])) );

Of course you should apply strval before pass it along like() function and verify the returned value

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • I'm not sure I follow. Strval returns the string value, but mongo still contains numerical values and the filter still fails. – ecc Sep 22 '14 at 15:01
  • So use intval() function. Maybe I understood the opposite :) – DonCallisto Sep 22 '14 at 15:26
  • Well, that will work most of the time, but the problem is how Mongo assumes number type for numerical strings, but string type for non numerical strings, which sometimes occur... – ecc Sep 22 '14 at 16:11