API Creator supports MongoDB. As a software engineer, you can use the MongoDB integration to:
Create a MongoDB Resource Type
Select Attributes to DisplayThe Attributes tab works with SQL Tables that are part of the Schema of the current project. You can select, alias, and format specific columns. The columns are reflected in the JSON returned by the REST API. In our example, we are asked to return:
Using the Resource editor and specification, we describe the data that we want (SQLCustomer is the parent resource and MongoOrders is the child resource). We create sub-resources attaching (e.g. joining) the sets at child level to create a new resource type. In this example, we pass the parent row attribute "<name>" joining the orders field in the Resource (see the Join box (customer_name: "<name>" ). Notice the syntax joins the child attribute to the parent value for each row returned from the parent. The filter is used to refine the selection to only the paid orders.Define the SQLCustomer Resource first { "name": "Alpha and Sons", "balance": 4484, "credit_limit": 9000, "@metadata": { "href": "http://localhost:8080/APIServer/rest/el-local/demo/v1/CustomerJoinMongoPurchaseOrders/Alpha%20and%20Sons", "checksum": "A:e86aea2e0a4e74bf" }, "MongoOrders": [ { "_id": { "$oid": "53d64c59a32268822c09e999" }, "order_number": 6, "amount_total": 108, "paid": false, "notes": "Pack with care - fragile merchandise", "customer_name": "Alpha and Sons", "salesrep_id": 7, "items": [ { "_id": { "$oid": "53d64c39a32268822c09e750" }, "lineitem_id": 11, "product_number": 2, "qty_ordered": 2, "product_price": 25, "amount": 50 }, Join two MongoDB CollectionsMongoDB developers are told that they cannot do joins across collections. In a strict language sense, this is true. MongoDB is not a relational database. In Live API Creator, resources are created dynamically and incrementally so a parent resource can pass detail row information to a child to retrieve a sub-resource of another collection. For example, we have one collection in MongoDB named department. We have been asked to return a new blended resource that includes these department records and employees that work in the department. These collections can be from different databases or even different servers. The 'join' passes selected attributes from the parent to the child resource to create a 'linked' collection of related values in the sub-resource. This allows you to do joins across MongoDB-to-MongoDB, MongoDB to SQL, SQL to MongoDB, and MongoDB to external REST for a blended mix and match of complex resource types. Note: The Syntax of the Join is expressed as a MongoDB expression: {department_name: "<name>"} where <name> is passed from the department collection. Attach PathIn the previous example, when we join a MongoDB collection, we need to tell the Resource logic where to insert this sub-resource collection. If the name is unique; then the result is a new attribute array with the result. If the name is an existing attribute array - the collection is inserted inside that array.This optional field can specify which keys (or attributes) should be included in the MongoDB resource. A simple example If your Mongo resource has attributes {name: 1, balance: 1}or you specify the opposite, i.e. do not include the age attribute: {age: 0} This field can actually do quite a bit more than that -- it is passed to MongoDB as the second parameter of the find method. For instance, you could use Mongo's projection capabilities, for example: { coordinates: {$slice: 2}}which would return only the first values from the array named "coordinates". FilterThis is a way to specify additional filtering on the resource, e.g. if you want to restrict the rows that should be returned by the resource.For SQL resources, this should simply be a fragment of a where expression; amount_total < 1000 and paid = 'Y' For MongoDB resources, you should use the Mongo syntax: : {"$and": [{"$lt": {"amount_total": 1000}}, {"paid": "Y"}]} REST Lab Example - GET MongoDepartment with EmployeesInDepartment [ { "_id": { "$oid": "53d64c02a32268822c09e5df" }, " name ": "Euro Sales", "managed_by": null, "head_department_name": "Sales", "sum_sub_department_budget": 400, "budget": 200, "budget_with_sub_department_budget": 600, "notes": "Dept Notes - Euro Sales", "secret_agenda": "Agenda - Euro Sales", "ts": { "$date": "2014-07-26T11:16:32.000Z" }, " employees ": [ { "_id": { "$oid": "53d64c03a32268822c09e5e9" }, "name": "Sami Stoner", "base_salary": 44000, "employee_type": "salesrep", "ts": { "$date": "2014-07-26T11:16:32.000Z" }, "visible_to": null, " department_name ": "Euro Sales", "on_loan_department_name": null, "notes": "on a high" } ] } ] Add Request/Response events to MongoDBUsing the API Creator additional pre-and post-event handling services can be added for your MongoDB resources. Event handling begins with incoming requests. The JavaScript editor and access to internal request state information (e.g. a GET, PUT, POST, DELETE):
See Request Events page for samples. A Resource Row Event is a piece of JavaScript that gets executed every time a row is retrieved from the database for a specific resource or sub-resource. This JavaScript code can then manipulate the row. For more information about row events, see row event. Business logic is triggered by PUT, POST, and DELETE changes to SQL data only which in turn can call MongoDB resources to include in calculations, validations, constraints, and decision support. Use Resources in Business RulesIn the blended architecture of the cloud, companies are turning to tools like MongoDB to build part of their new web and mobile strategy:
Authentication and Access ControlLive API Creator offers the following authentication services: LDAP, Microsoft Azure Active Directory (Azure AD), and SQL. In addition, the Role-Based Access Control service feature gives fine-grain control to determine who can see which resource based on role assignment. Advanced Access to MongoDB using JavaScriptThe native MongoDB Java driver can be used to write advanced (native) JavaScript. In this example, we get the orders for a specific customer (containingRow) and passing this to MongoDB directly and returning the JSON response. API Creator includes a JavaScript wrapper library around the MongoDB Java driver to make the process of using JavaScript easier. See the MongoUtility link. MongoDB and JDBC (Experimental)There are several JDBC drivers that can connect to MongoDB and perform selects, joins, order by, group by, and filtered where clauses. This example is shown using one of these drivers. The JDBC driver is loaded using the Project/Library upload facility to add new driver JAR files and optionally any JavaScript into the system. This example is using a JDBC driver to connect to MongoDB and execute an SQL query with a where clause. The JDBC driver can also do joins, sorts, column selection and some drivers allow selection into nested arrays (e.g Customer.Orders.Items.PartNum = 12345); |
Docs > API Creator > Create your API Project > Define Custom REST Resources > Language Examples > Data Integration >