Skip to content
Uwe Fetzer edited this page Sep 5, 2013 · 2 revisions

Usage of moDBap

Insert records into DB (you can pass any valid ABAP [deep] structure/table)

DATA: connection TYPE REF TO zcl_mongo_connection,
  	db     	TYPE REF TO zcl_mongo_db,
  	collection TYPE REF TO zcl_mongo_collection,
  	scarr_t	TYPE STANDARD TABLE OF scarr.
 
SELECT * FROM scarr
  INTO TABLE scarr_t.
 
connection = zcl_mongo_connection=>connect( '192.168.38.56' ).
db = connection->db( 'sap' ).
collection = db->collection( 'scarr' ).
collection->insert( scarr_t ).

You can always check the results with the mongo DB interactive shell ("./mongo"):

> use sap
switched to db sap
> db.scarr.find()
{ "_id" : ObjectId("4f37a5ec476ef70158000000"), "mandt" : "001", "carrid" : "AA"
, "url" : "http://www.aa.com", "currcode" : "USD", "carrname" : "American Airlin
es" }
{ "_id" : ObjectId("4f37a5ec476ef70158000001"), "mandt" : "001", "carrid" : "AB"
, "url" : "http://www.airberlin.de", "currcode" : "EUR", "carrname" : "Air Berli
n" }
...

Get records from the DB

Get as ABAP data:

collection->find( IMPORTING data = scarr_t ).

Get as JSON string:

collection->find( IMPORTING result = result ).

Get one record:

DATA: scarr_s TYPE scarr.
 
collection->find_one(
  EXPORTING
	criteria = '{"carrid":"LH"}'
  IMPORTING
	data 	= scarr_s
	).

The database cursor is set automatically. If more than one record matches the criteria you can read the next record with "find_more( )" in a loop.

Update records

collection->update(
  EXPORTING
	criteria = '{"carrid":"LH"}'
	newobj   = '{"$set":{"url":"http://www.se38.de"}}'
).

You pass the whole object (as JSON) or use the following modifier operations (see docu on http://www.mongodb.org/display/DOCS/Updating )

$inc
$set
$unset
$push
$pushAll
$addToSet and $each
$pop
$pull
$pullAll
$rename
$bit

Update using an ABAP object:

collection->update(
  EXPORTING
	criteria = '{"carrid":"LH"}'
	data 	= scarr_s ).

Remove records

result = collection->remove( '{"carrid":"LH"}' ).

Count entries

entries = collection->count( ).

Get distinct values

result = collection->distinct( 'carrid' ).

Group by

result = collection->group(
       	key   	= '"carrid":true'
       	reduce	= 'prev.sumseats += doc.seatsmax - doc.seatsocc; if (doc.seatsocc == 0){ prev.nullseats++ };'
       	initial   = '"sumseats":0,"nullseats":0'
       	condition = '"fldate":{"$gt":"20110101"}'
       	).

Dropping a collection

result = collection->drop( ).

Performing DB commands

result = db->cmd( '{"buildinfo":1}' ).

Authentication

background:

result = db->auth( user = user password = password ).

Clone this wiki locally