Using '$or' criteria #26

Closed
russellseymour opened this Issue Jan 17, 2012 · 5 comments

Comments

Projects
None yet
3 participants
@russellseymour

Hello again,

I am trying to run an '$or' query with Morph. However although the query seems ok (by using getRawQuery) and can be executed against Mongo from the command line, I do not seem to be getting any results back from Morph.

I am using the following construct:

$query -> property('$or') -> equals(array(array('name' => 'layout)))

When I JSON encode the '$query -> getRawQuery()' I get the following:

{"$or":[{"name":"layout_www"}]}

This JSON snippet returns the expected result when I use the MongoDB command line. However I do not get anything back in my object from Morph.

Is it possible to use '$or' with Morph? If so is this the correct way of doing it? If not is there a way to perform an '$or' query using Morph?

Thanks for your quick responses and help with my queries. It is very much appreciated.

Russell

@russellseymour

This comment has been minimized.

Show comment
Hide comment
@russellseymour

russellseymour Jan 17, 2012

Please ignore my last post.
I retested the code and it does work.

Sorry for posting something that was not a problem.

Russell

Please ignore my last post.
I retested the code and it does work.

Sorry for posting something that was not a problem.

Russell

@a-musing-moose

This comment has been minimized.

Show comment
Hide comment
@a-musing-moose

a-musing-moose Jan 17, 2012

Owner

Hi Russel,

The property() function is for object properties not operators. Sadly
Morph was written before the $or operator existed and as such isn't
really supported.

Also, you shouldn't actually need to call getRawQuery() at all. You can
pass the Query object directly to the findByQuery function.

Basically $or has not really been tested. I am also not sure exactly
what you are trying to achieve, from that query a simple equals would work:

`````` $query->property('name')->equals('layout')```

or if you wanted an or on the name property you can do:

$query->property('name')->in(array('layout', 'a_n_other'))

Regards,
Jon

Hello again,

I am trying to run an '$or' query with Morph. However although the query seems ok (by using getRawQuery) and can be executed against Mongo from the command line, I do not seem to be getting any results back from Morph.

I am using the following construct:

$query -> property('$or') -> equals(array(array('name' => 'layout)))

When I JSON encode the '$query -> getRawQuery()' I get the following:

{"$or":[{"name":"layout_www"}]}

This JSON snippet returns the expected result when I use the MongoDB command line. However I do not get anything back in my object from Morph.

Is it possible to use '$or' with Morph? If so is this the correct way of doing it? If not is there a way to perform an '$or' query using Morph?

Thanks for your quick responses and help with my queries. It is very much appreciated.

Russell


Reply to this email directly or view it on GitHub:
#26

--
Jonathan Moss
Director

Tangent / One Australia Pty
Suite 109
175 Sturt Street
Melbourne, VIC 3006
ABN: 76 137 883 615
T: +61 (0) 3 9645 8796
M: +61 (0) 4 1525 1208
E: jonathan.moss@tangentone.com.au

Owner

a-musing-moose commented Jan 17, 2012

Hi Russel,

The property() function is for object properties not operators. Sadly
Morph was written before the $or operator existed and as such isn't
really supported.

Also, you shouldn't actually need to call getRawQuery() at all. You can
pass the Query object directly to the findByQuery function.

Basically $or has not really been tested. I am also not sure exactly
what you are trying to achieve, from that query a simple equals would work:

`````` $query->property('name')->equals('layout')```

or if you wanted an or on the name property you can do:

$query->property('name')->in(array('layout', 'a_n_other'))

Regards,
Jon

Hello again,

I am trying to run an '$or' query with Morph. However although the query seems ok (by using getRawQuery) and can be executed against Mongo from the command line, I do not seem to be getting any results back from Morph.

I am using the following construct:

$query -> property('$or') -> equals(array(array('name' => 'layout)))

When I JSON encode the '$query -> getRawQuery()' I get the following:

{"$or":[{"name":"layout_www"}]}

This JSON snippet returns the expected result when I use the MongoDB command line. However I do not get anything back in my object from Morph.

Is it possible to use '$or' with Morph? If so is this the correct way of doing it? If not is there a way to perform an '$or' query using Morph?

Thanks for your quick responses and help with my queries. It is very much appreciated.

Russell


Reply to this email directly or view it on GitHub:
#26

--
Jonathan Moss
Director

Tangent / One Australia Pty
Suite 109
175 Sturt Street
Melbourne, VIC 3006
ABN: 76 137 883 615
T: +61 (0) 3 9645 8796
M: +61 (0) 4 1525 1208
E: jonathan.moss@tangentone.com.au

@russellseymour

This comment has been minimized.

Show comment
Hide comment
@russellseymour

russellseymour Jan 18, 2012

Evening Jon,

Thanks for getting back to me.

I am trying to testing different fields for the same value. For example (excuse the SQL syntax)

SELECT name FROM profile WHERE id = '1234' OR nickname = '1234'

The code snippet that I posted does actually work, and it works with more than one field as I require. So what I have ended up with is as follows:

$query -> property('$id') -> equals(array(array("_id" => $template), array("name" => $template)))

and this does work, although I understand from what you say that it is not really supported in this way.

I used the getRawQuery() method to show me what the findByQuery() would actually execute on the Mongo server, it was just a way for me to test things.

Hope that helps clear it up a bit.

Thanks, Russell

Evening Jon,

Thanks for getting back to me.

I am trying to testing different fields for the same value. For example (excuse the SQL syntax)

SELECT name FROM profile WHERE id = '1234' OR nickname = '1234'

The code snippet that I posted does actually work, and it works with more than one field as I require. So what I have ended up with is as follows:

$query -> property('$id') -> equals(array(array("_id" => $template), array("name" => $template)))

and this does work, although I understand from what you say that it is not really supported in this way.

I used the getRawQuery() method to show me what the findByQuery() would actually execute on the Mongo server, it was just a way for me to test things.

Hope that helps clear it up a bit.

Thanks, Russell

@a-musing-moose

This comment has been minimized.

Show comment
Hide comment
@a-musing-moose

a-musing-moose Jan 19, 2012

Owner

Hey russellseymour,

Yeah - That's fine. If you can think of a way to allow for $or queries in a more elegant way that would be good. Or even better fork the project, implemented the changes and issue a pull request :)

maybe something as simply as:

$query->or($query1, $query2);

Jon

Owner

a-musing-moose commented Jan 19, 2012

Hey russellseymour,

Yeah - That's fine. If you can think of a way to allow for $or queries in a more elegant way that would be good. Or even better fork the project, implemented the changes and issue a pull request :)

maybe something as simply as:

$query->or($query1, $query2);

Jon

@daniphp

This comment has been minimized.

Show comment
Hide comment
@daniphp

daniphp Sep 24, 2012

Contributor

I made a method:
daniphp/morph@3912cbc

Contributor

daniphp commented Sep 24, 2012

I made a method:
daniphp/morph@3912cbc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment