MongoDB Insert

1
0
-1

Hi everyone,
can anybody help me to configure my process with the Mongo Database?
a small example to insert data will be very suitable for me.
Thanks for the help!
Asma Hassani

1 answer

1
0
-1
This one is the BEST answer!
  1. Install MongoDB
  2. Copy the MongoDB-Driver to ..\workspace\tomcat\server\lib (available from http://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/in...), don't forget the dependencies as well... :)
  3. Open Studio
  4. Create a New Diagram and Rename it to testMongoDB
  5. Goto Developement->Manage JARS and ADD the MongoDB Driver JAR(s)
  6. Click Step 1 and go to Details->Execution Tab
  7. Add a new Script Connector

Use the following script:

//Logging
import org.bonitasoft.engine.api.ProcessRuntimeAPI;
import java.util.logging.Logger;
import java.lang.StringBuilder;

//MongoDB Support
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

int dI = 0;
boolean debug = true;
StringBuilder logWrite = new StringBuilder();
Logger logger = Logger.getLogger("org.bonitasoft");

ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
if(debug){logger.severe("\n\n");}

//set the name of the routine
if(debug){logger.severe("Process Name: "+ processRuntimeAPI.getProcessInstance(processInstanceId).getName());}
if(debug){logger.severe("\n\t" + dI++ + " Trace Start");}

//TODO - Code goes in here - START

MongoClient mongo = new MongoClient( "localhost" , 27017 );
if(debug){logger.severe("\n\t" + dI++ + " got a connection");}

DB db = mongo.getDB("myTestMongoDB");
if(debug){logger.severe("\n\t" + dI++ + " opened my Database");}

DBCollection table = db.getCollection("user");
if(debug){logger.severe("\n\t" + dI++ + " opened my Table");}

BasicDBObject document = new BasicDBObject();
if(debug){logger.severe("\n\t" + dI++ + " opened my Record");}

document.put("name", "mkyong");
document.put("age", 30);
document.put("createdDate", new Date());
if(debug){logger.severe("\n\t" + dI++ + " put my Data");}

table.insert(document);
if(debug){logger.severe("\n\t" + dI++ + " record Saved");}

//TODO - Code goes in here - END

if(debug){logger.severe("\n\t" + dI++ + " Trace End");}
if(debug){logger.severe("\n\n");}

  1. Execute

Hope that helps,

regards
Seán

PS: As this reply answers your question, please mark as resolved.

Comments

Submitted by asmahassani on Sun, 04/09/2017 - 00:19

Thanks Sean,
I followed the proposed steps
How can I verify that it works successfully?

Submitted by Sean McP on Sun, 04/09/2017 - 01:54

if you execute the process and then look at the log you should see the information there...

regards

Submitted by asmahassani on Sun, 04/09/2017 - 11:44

for the second step that you have mentioned:
i have this path C:\BonitaBPMCommunity-7.3.1\workspace\tomcat\lib
in wich one of the JAR files should i copy the MongoDB driver? or should i create a new file?

Submitted by Sean McP on Mon, 04/10/2017 - 00:37

Just copy the jar from the download location given, my path is for 7.4. You should use C:\BonitaBPMCommunity-7.3.1\workspace\tomcat\lib for 7.3

regards

Submitted by asmahassani on Wed, 04/12/2017 - 13:16

i have as result:
a new database has been created: MytestMongoDb, a collection user with 3 documents: name, age and created date.
does this mean that my connector works fine?
but when i add another human task, the process is blocked in the service task and nothing is displayed.

please, any suggestion?

Submitted by Sean McP on Thu, 04/13/2017 - 04:58

a new database has been created: MytestMongoDb, a collection user with 3 documents: name, age and created date.
does this mean that my connector works fine?

I would say so...yes.

but when i add another human task, the process is blocked in the service task and nothing is displayed.

Well it's something to do with this task, you will have to determine what is in the new task to figure out what is wrong. What does the log say?

Submitted by asmahassani on Thu, 04/13/2017 - 11:29

after the service task, i have a human task with a simple message displayed.
nothing complicated !!

Notifications