How to query by datetime in Doctrine MongoDB ODM?

Is it possible to use a SQL style 'union' and 'pseudo column' in a MongoDB query?

  • Can the following sql query be done as a mongo query?  (this would solve a big problem for me) select * from ( select attribute1, '100' from collection where attribute2 = 1 union select attribute1, '200' from collection where attribute2 = 2 )  where rownum between 1 and 20    order by attribute1

  • Answer:

    Something like this? db.collection.find({$or: [ {attribute2: 1}, {attribute2: 2}]}).sort({attribute1:1}).limit(20)

Harry Heymann at Quora Visit the source

Was this solution helpful to you?

Other answers

So MongoDB doesn't have the "pseudo column" in the query language. However, I'm not clear that your query actually requires either the "pseudo column" or the "union". The "pseudo column" is just a function of attribute2 and the union is basically an "OR" clause. Did you mean "order by pseudo column"?

Gaëtan Voyer-Perrault

Take a look at the Aggregation framework in 2.1.0 and above. http://www.mongodb.org/display/DOCS/Aggregation+Framework This would allow for the projection of the '100' and '200' values during the processing.

Alvin Richards

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.