v1.10

Document

How to Start SequoiaDB

Basic Operation

Data Model

Database Management

Aggregation

Index

Replication

Cluster

Connector

Reference

Development

Release Note

FAQ

  • Content
  • Comments
  • History

Query

Of the basic database operations(i.e.CRUD), read operatios are these those retrieve records or documents from a collection in SequoiaDB, and include all operations that return a cursor in response to application request data.

This dacument describes the syntax and structure of the queries applications use to request data from SequoiaDB and how different factors affect the efficiency of reads.

Note:

All of the examples in this document use the SequoiaDB shell interface.

find()

we use findmethod to read records from SequoiaDB. the findmethod is the main method to select records from collections and it returns a cursor which contains a lot of records. it's grammatical structure as follows:

db.collectionspace.collection.find([cond],[sel])

The corresponding opration in sql: the method of find() is simalar with select statement :

. the [cond] parameter corresponds to where statement

. the [sel] parameter corresponds to the list of fields in the result set

there is a record in collection like this:

    {
  "_id":1,
  "name":{
     "first" : "Tom",
     "second":"David"
       },
  "age":23
  "birth":"1990-04-01",
  "phone":[
      10086,
      10010,
      10000
     ],
  "family":[
      {
     "Dad":"Kobe",
     "phone":139123456
      },
      {
      "Mom":"Julie",
      "phone":189123456
       }
    ]
 }     

return all the records from the collection

If there is no cond parameter, the method of db.collectionspace.colletion.find() returns all the records from the collection. The following operation select all records in the collection named bar which in the collectionspace named foo:

db.foo.bar.find()

return records that match query conditions

  • Equality match

    The following operation select the records in the collection bar where the age filed has the value 23

     db.foo.bar.find({age:23})
  • use operators

    The following operation select the records in the collection bar where the age field is more than($gt)20

    db.foo.bar.find({age:{$gt:20}})
  • On array

    (1)query an element, The following operation returns a cursor to all records in the bar collection where the array field phone contains the element '10086':

     db.foo.bar.find({"phone":10086})

    (2)query array element which is BSON Object. The following operation return a cursor to all records in the bar collection where family array contains a subdocument element that contains the Dad field equal to 'Kobe' and the phone field equal to '139123456':

     db.foo.bar.find( 
      {
      "family":{
        $elemMatch: {
         "Dad":"Kobe",
         "phone":139123456
       }
      }
      })
  • query on subdocument

    The following operation returns a cursor to all documents in the bar collection where the subdocument name is exactly {first:"Tom"}

     db.foo.bar.find(
        {
         "name":{ 
         "first":"Tom"
          }
        }
       )
    It can also be written as:
     db.foo.bar.find(
         {
         "name.first":"Tom"
         }
       )

specity the field of the returned records

If specity the sel parameter of find method, then only return the field within sel parameter:

db.foo.bar.find(null,{name:""})
Note:

If a records doesn't contain the specity field(as:people), SequoiaDB will default return. For example:

db.foo.bar.find({},{name:"",people:""})
return:{
       "name":{
                "fist":"Tom",
                "second":"David"
              },
        "people":""
        }

find() more information

Execute db.foo.bar.find().help(), then will see more methods of find()

  • cursor.sort()

    The method of sort() is used to order by the specified field, syntax: sort({"field1":1|-1,"field2":1|-1,...}), '1' stand for Ascending, '-1' stand for Descending. If the the method of find doesn't specity the content of sel parameter, then sort() will order by the sort parameter, but if sel parameter specity the returned field, then sort() can only order by the field included in the sel parameter. For example:

    db.foo.bar.find().sort({age:1})   //return all the records in the bar collection and order by the value of age field in ASC
    db.foo.bar.find(null,{name:""}).sort({age:1})  //this operation actually can't reach the purpose of sorting.
  • cursor.hint()

    add indexes to speed up the query speed, assuming there is a index named 'testIndex':

    db.foo.bar.find().hint({"":"testIndex"})
  • cursor.limit()

    limit the number of records in the result set:

    db.foo.bar.find().limit(3) //return the first three records in the result set
  • cursor.skip()

    the skip() method controls the point of the results set, that is skipping the first 'num' records, begin to return from the 'num+1' records:

    db.foo.bar.find().skip(5)  //skipping the first 5 records in the bar collection
  • use cursor to control the returned records

    db.foo.bar.find().current() //return the record that the current cursor point to 
    
    db.foo.bar.find().next() //return the next record that the current cursor point to 
     
    db.foo.bar.find().count()//return the number of records that the current cursor point to
                
    db.foo.bar.find().size() //return the distance from the current cursor to the final cursor
                
    db.foo.bar.find().toArray() //return the result sets with array form
please login to comment.
Latest Comment
  • 2015-02-14

About Us

SequoiaDB is a financial-level distributed database vendor and is the first Chinese database listed in Gartner’s Magic Quadrant OPDBMS report. SequoiaDB has recently released version 3.0.
SequoiaDB is now penetrating the vertical sector Financial Industry quickly and had more than 50 banking clients and hundreds of enterprise customers in industries including government, telecommunication, Internet and IoT.

Beijing:
Tower R, No.8 North Star East Road, Chaoyang District, Beijing,China
Guangzhou:
Tower A, No.22 Qinglan Street, Panyu District, Guangzhou,China
Shenzhen:
Tsing Hua Tech Park, Nanshan District, Shenzhen,China
Tel:400-8038-339
E-mail:contact@sequoiadb.com