querying.criteria-object.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. Title: The EMongoCriteria Object
  2. Author: Dariusz Górecki <darek.krk@gmail.com>
  3. ---
  4. By default MongoDB requires to build advanced query arrays if you want advanced search for documents in DB.
  5. The EMongoCriteria object simplifies process of building this query arrays.
  6. First of all create the object instance: `$criteria = new EMongoCriteria();`
  7. You may define query criteria in three simple ways:
  8. 1. Simple 'equlas' condition, that is equivalent of SQL: `WHERE fieldName = 'fieldValue'`:
  9. - `$criteria->fieldName = 'fieldValue';`
  10. 2. By field name call ie. SQL: `WHERE fieldName > 1234`:
  11. - `$criteria->fieldName('>', 1234);`
  12. 3. By addCond method ie. SQL: `WHERE fieldName <= 1234`:
  13. - `$criteria->addCond('fieldName', '<=', 1234);`
  14. You can add multiple conditions, using any of the above techniques, they will be mixed together.
  15. In addition to above the criteria object has additional special methods.
  16. 1. `limit($value)` that takes as parameter the query results limit, same as SQL `LIMIT` directive
  17. 2. `offset($value)` that takes as parameter the query results offset to get, same as SQL `LIMIT` directive second parameter
  18. 3. `sort($fieldName, $sortDirection)` this will be covered shortly
  19. 4. `select($array)` this will be covered shortly
  20. > [information]
  21. > If you want to add simple criteria, that will use field name that is a method name from the above list,
  22. > you can use the addCond method ie. `$criteria->addCond('limit', '==', 1234);`
  23. ## sort EMongoCriteria method {#sort}
  24. The sort method takes two parameters
  25. - first is a field name, to use for sorting
  26. - second is a sort direction, witch may be one of:
  27. 1. `EMongoCriteria::SORT_ASC` for ascending sorting (positive value 1)
  28. 2. `EMongoCriteria::SORT_DESC` for descending sorting (negative value -1)
  29. You can call sort method as many times as you wish, the sorting criteria will be mixed together.
  30. ## select EMongoCriteria method {#select}
  31. The select method takes only one argument, array of field names.
  32. By default MongoDB will return all fields from of document, use this method to tell mongo that
  33. you want only specified field list.
  34. > [important]
  35. > - MongoDB regardless of anything, will always return the _id field within result sets
  36. > - You can call sort method multiple times, all calls will be merged using array_merge function
  37. ## List of supported operators {#oplist}
  38. List consist of operators and after `|` available shortcut (if any)
  39. > [information]
  40. > Operators are case-insensitive
  41. ~~~
  42. - 'greater' | >
  43. - 'greaterEq' | >=
  44. - 'less' | <
  45. - 'lessEq' | <=
  46. - 'notEq' | !=, <>
  47. - 'in' |
  48. - 'notIn' |
  49. - 'all' |
  50. - 'size' |
  51. - 'exists' |
  52. - 'notExists' |
  53. - 'type' | // BSON type see mongodb docs for this
  54. - 'mod' | %
  55. - 'equals' | ==
  56. - 'elemMatch' |
  57. - 'or' |
  58. ~~~
  59. > [warning]
  60. > the $or operator in newer versions of mongodb. It works only with versions MongoDB 1.5.3+
  61. For examples and use for how to use these operators effectively, use the [MongoDB Operators Documentation here](http://www.mongodb.org/display/DOCS/Advanced+Queries).
  62. ## Examples: {#examples}
  63. ~~~
  64. [php]
  65. // first you must create a new criteria object
  66. $criteria = new EMongoCriteria;
  67. // find the single user with the personal_number == 12345
  68. $criteria->personal_number('==', 12345);
  69. // OR like this:
  70. $criteria->personal_number = 12345;
  71. $user = User::model->find($criteria);
  72. // find all users in New York. This will search in the embedded document of UserAddress
  73. $criteria->address->city('==', 'New York');
  74. // Or
  75. $criteria->address->city = 'New York';
  76. $users = User::model()->findAll($criteria);
  77. // Ok now try this. Only active users, only show at most 10 users, and sort by first name, descending, and offset by 20 (pagination):
  78. // note the sort syntax. it must have an array value and use the => syntax.
  79. $criteria->status('==', 1)->limit(10)->sort(array('firstName' => EMongoCriteria::SORT_DESC))->offset(20);
  80. $users = User::model()->findAll($criteria);
  81. // A more advanced case. All users with a personal_number evenly divisible by 10, sorted by first name ascending, limit 10 users, offset by 25 users (pagination), and remove any address fields from the returned result.
  82. $criteria->personal_number('%', array(10, 0)) // modulo => personal_number % 10 == 0
  83. ->sort(array('firstName' => EMongoCriteria::SORT_ASC))
  84. ->limit(10)
  85. ->offset(25);
  86. $users = User::model()->findAll($criteria);
  87. //using $or operator
  88. //Find all contacts where the FirstName can be Paris OR City can be Paris
  89. $criteria = new EMongoCriteria();
  90. $criteria->addCond('contactFirstname', 'or', 'Paris');
  91. $criteria->addCond('contactCity', 'or', 'Paris');
  92. $contacts = ContactsDb::model()->findAll($criteria);
  93. ~~~
  94. ##Regexp / SQL LIKE replacement {#regexp}
  95. You can use native PHP Mongo driver class MongoRegex, to query:
  96. ~~~
  97. [php]
  98. // Create criteria
  99. $criteria = new EMongoCriteria;
  100. // Find all records witch have first name starring on a, b and c, case insensitive search
  101. $criteria->first_name = new MongoRegex('/[abc].*/i');
  102. $clients = Client::model()->findAll($criteria);
  103. // see phpdoc for MongoRegex class for more examples
  104. ~~~
  105. ##Creating criteria object from an array: {#array}
  106. ~~~
  107. [php]
  108. // Example criteria
  109. $array = array(
  110. 'conditions'=>array(
  111. // field name => operator definition
  112. 'FieldName1'=>array('greaterEq' => 10), // Or 'FieldName1'=>array('>=' => 10)
  113. 'FieldName2'=>array('in' => array(1, 2, 3)),
  114. 'FieldName3'=>array('exists'),
  115. ),
  116. 'limit'=>10,
  117. 'offset'=>25,
  118. 'sort'=>array('fieldName1' => EMongoCriteria::SORT_ASC, 'fieldName4' => EMongoCriteria::SORT_DESC),
  119. );
  120. $criteria = new EMongoCriteria($array);
  121. // or
  122. $clients = ClientModel::model()->findAll($array);
  123. ~~~