Skip to content

Query the Database

topofocus edited this page Jul 24, 2019 · 10 revisions

ActiveOrient supports querying on the Mode-l and on the Record-Level. The interface is identical.

 '#23:0'.expand.query.nodes(:out, via: /on/ ).execute

 TestModel.query.where( d: 5).limit(1).execute

query returns a pre-configured OrientSupport::OrientQuery-Object. Any directive can be added as parameters or chained through setter-methods.

  q =  TestModel.query( order: "@rid", limit:1)

is identical to

  q =  TestModel.query.order("@rid").limit( 1 )

The query is compiled via compose or to_s and transmitted by execute

 > TestModel.query.order("@rid").limit( 1 ).to_s
 => "select from testModel order by @rid limit  1" 
 > TestModel.query.order("@rid").limit( 1 ).execute.to_human  
  # INFO->select from testModell order by @rid limit  1
 => ["<TestModell[26:0]: a : 7, b : 8, test : 8>"] 

The result of a database-query is always an array. If only one record is expected as answer, execute can be called with the parameter reduce: true

 > TestModell.query.order("@rid").limit( 1 ).execute(reduce: true).to_human
 => "<TestModell[26:0]: a : 7, b : 8, test : 8>" 

executeexposes any raw-result-element to an optional block and returns the collected modifications.

 > TestModell.query.projection( 'b*a' => 'g').execute
 # INFO->select b*a as g from testModel 
 => ["{ g: 56 }", "{ g: 66 }", "{ g: 55 }", "{ g: 46 }"] 
 > TestModel.query.projection( 'b*a as g').execute{|n| n[:g]}
 => [56, 64, 72, 80]
 > TestModel.query.projection( 'b*a' =>'g').projection( 'a' => :id ).execute{|n| {n[:id] => n[:g]}} 
 => [{7=>56}, {8=>64}, {9=>72}, {10=>80}] 

Clone this wiki locally