Query Grammar

In this section:

The contents of a query are a set of key-value pairs. You can join multiple pairs together to form and or ortype queries. Furthermore, you can also use regular expressions for the value part of a pair.


Top of page

x
Searching Elements

Consider the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
   <a>
      <b>test</b>
      <c>12345</c>
   </a>

To find all document ids for which the value of <b> is equal to test, use the search string {"a.b":"test"} ,{"_id":1}. The HTTP request is as follows:

http://localhost:2222/?query={%22a.b%22:%22test%22},{%22_id%22:1}

Top of page

x
Searching Attributes

In the iWay XML Archive, XML documents are first converted to JSON before they are loaded into the archive. This notation is used for storage and retrieval efficiency. Changes that convert attributes to child elements are made to the XML structure to support this notation.



Example: Attribute Search

Consider the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
   <a name="bob">
      <b>test</b>
      <c>12345</c>
   </a>

To find all document ids for which the value of the name attribute is equal to bob, use the search string {"a.name":"bob"} ,{"_id":1}. The HTTP request is as follows:

http://localhost:2222/?query={%22a.name%22:%22bob%22},{%22_id%22:1}

Top of page

x
Searching on Multiple Keys (and)

To search for a document based on multiple keys, type comma-delimited, key-value pairs in the search box. This is similar to an and in a SQL where clause.



Example: Multiple Key Search

Consider the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
   <a name="bob">
      <b>test</b>
      <c>12345</c>
   </a>

To find all documents in which the name attribute of <a> is equal to bob and the value of <b> is equal totest, type {"a.name":"bob","a.b":"test"},{"_id":1}. The HTTP request is as follows:

http://localhost:2222/
?query={%22a.name%22:%22bob%22,%22a.b%22:%22test%22},{%22_id%22:1}

Top of page

x
Advanced Queries

The iWay XML Archive query language is based on the mongoDB query language. Many of the advanced queries are supported. Some of the advanced queries require extra operators to complete, such as the or query. For more information on the mongoDB advanced queries, see http://www.mongodb.org/display/DOCS/Advanced+Queries



Example: Advanced Query

Consider the following XML documents:

<?xml version="1.0" encoding="UTF-8"?>
   <a name="bob">
      <b>test</b>
      <c>12345</c>
   </a>
<?xml version="1.0" encoding="UTF-8"?>
   <a name="bob">
      <b>testA</b>
      <c>12345</c>
   </a>

To find all document ids for which the value of <b> is equal to test or testA, use the following syntax:

{$or:[{"a.b":"test"},{"a.b":"testA"}]} ,{"_id":1}
The HTTP request is as follows:

http://localhost:2222/
?query={$or:[{%22a.b%22:%22test%22},{%22a.b%22:%22testA%22}]},
{%22_id%22:1}

Top of page

x
Using Regular Expressions

You can use Pearl Compatible Regular Expressions (PCRE) for the value of a search expression. The expressions are not enclosed in quotation marks, and are bound between / (slash) characters. For more information on PCRE, see http://www.cs.tut.fi/~jkorpela/perl/regexp.html



Example: Regular Expression

Consider the following XML documents:

<?xml version="1.0" encoding="UTF-8"?>
   <a name="bob">
      <b>test</b>
      <c>12345</c>
   </a>
<?xml version="1.0" encoding="UTF-8"?>
   <a name="bob">
      <b>testA</b>
      <c>12345</c>
   </a>

To find all document ids for which the value of <b> contains the string test, type the following syntax

{"a.b":/test./},{"_id":1}
The HTTP request is as follows:

http://localhost:2222/?query={%22a.b%22:/test./},{%22_id%22:1}

Top of page

x
Searching by _id

The previous examples limited the results of a search to return on the _id key-value pair. If the limitation is removed, the entire JSON document is retrieved for each result. If you search for a single id, you the entire JSON document for that id is retrieved. To execute a search by id, you must use the mongoDB ObjectId class in your query.

For example, to return a document with the id "4d068547e818f7cba1b63688", type the following search term:

{"_id":new ObjectId("4d068547e818f7cba1b63688")}
The HTTP request is as follows:

http://localhost:2222/
?query={%22_id%22:new%20ObjectId(%224d49af7ce5397929bad5421e%22)}

iWay Software