1. Suppose we have a MongoDB table with following records:
> db.users.find();
{ "_id" : ObjectId("4f65967bd7fdfafd4d45cece"), "name" : "Test User1", "age" : 1
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45cecf"), "name" : "Test User2", "age" : 2
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced0"), "name" : "Test User3", "age" : 3
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced1"), "name" : "Test User4", "age" : 4
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced2"), "name" : "Test User5", "age" : 5
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced3"), "name" : "Test User6", "age" : 6
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced4"), "name" : "Test User7", "age" : 7
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced5"), "name" : "Test User8", "age" : 8
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced6"), "name" : "Test User9", "age" : 9
}
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced7"), "name" : "Test User10", "age" :
10 }
2. Query MongoDB by following code:
//Transfer query language => EMF query model
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
Injector injector = new MongoSQLStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mongosql"));
InputStream in = new ByteArrayInputStream("SELECT * FROM mongo://localhost:27017/tigergui/users WHERE name='Test User7' AND age>2".getBytes());
try {
resource.load(in, resourceSet.getLoadOptions());
} catch (IOException e) {
e.printStackTrace();
}
ModelImpl model = (ModelImpl) resource.getContents().get(0);
//Transfer EMF query model => DBObject
Mongo m = null;
try {
m = new Mongo( model.getDb().getUrl(), (int) model.getDb().getPort());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
DB db = m.getDB(model.getDb().getDbName());
DBCollection coll = db.getCollection(model.getDb().getName());
BasicDBObject query = (BasicDBObject) tansferModule(model);
//Query MongoDB by DBObject
DBCursor cur = coll.find(query);
while(cur.hasNext()) {
System.out.println(cur.next());
}
3. Query result:
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced4"} , "name" : "Test User7" , "age" : 7}
And if we change the SQL to "SELECT * FROM mongo://localhost:27017/tigergui/users WHERE age>2", the search result will be:
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced0"} , "name" : "Test User3" , "age" : 3}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced1"} , "name" : "Test User4" , "age" : 4}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced2"} , "name" : "Test User5" , "age" : 5}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced3"} , "name" : "Test User6" , "age" : 6}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced4"} , "name" : "Test User7" , "age" : 7}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced5"} , "name" : "Test User8" , "age" : 8}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced6"} , "name" : "Test User9" , "age" : 9}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced7"} , "name" : "Test User10" , "age" : 10}