Currently, we've our application on Play 1.2.5 with MySQL on the backend. We're trying to port this system to MongoDB using Morphia.
We're having problem while converting the MySQL Join Queries to MongoDB that uses Morphia. Following is the SQL query:
SELECT jobs.postal, count(distinct timesegments_id), group_concat(timesegments_id order by timesegments_id) as types, latitude, longitude FROM jobs
left join geocodes on jobs.geocode_id = geocodes.id left join jobs_timesegments on jobs_timesegments.jobs_id = jobs.id
where geocodes.last_updated is not null
and longitude >0
and timesegments_id is not null
group by jobs.postal;
Following is what we tried:
List<models.Geocode> Geocodelist = models.Geocode.find().filter("longitude >",0).filter("last_updated",new BasicDBObject("$ne","last_updated")).asList();
List<String> geocodeids = new ArrayList<String>();
for (models.Geocode geocode : Geocodelist) {
geocodeids.add(geocode.getIdAsStr());
}
List<models.Job> job = models.Job.find().filter("geocode_id",new BasicDBObject("$in",geocodeids)).asList();
List<String> jobids = new ArrayList<String>();
for (models.Job joblist : job) {
jobids.add(joblist.getIdAsStr());
}
List<models.Jobs_TimeSegments> timesegments_data = models.Jobs_TimeSegments.find().filter("jobs_id",new BasicDBObject("$in",jobids)).asList();
The above fetches three lists; however, we're not sure how to reference them to each other and get the list of columns that we want.
Any help is highly appreciated.
Thanks
Related
query should be sorted by 'lastUsedTimestamp' in ASC. If these entities lastUsedTimestamp is null or expired or do not have the field, we just remove them from the collection with defined limit.
I have them written like below but it is giving null
Criteria fieldsCriteria1 = Criteria.where("lastAccessTimestamp").lte(date);
Criteria fieldsCriteria2 = Criteria.where("lastAccessTimestamp").exists(false);
Query query2 = new Query();
query2.limit(3);
query2.with(Sort.by(Sort.Direction.ASC,"lastAccessTimestamp")); // to set in ASC order
query2.addCriteria(fieldsCriteria1); // to set the expired time
If i have only these above criteria added it works fine, the problem occurs when i add the below criteria
query2.addCriteria(fieldsCriteria2); // to get if the lastAccessTimestamp field is empty
I am new to Mongo Db, also I am not sure which is the best way to fulfill the above query.
I had figured out the solution, the below query worked for me.
var fieldsCriteria = new Criteria()
.orOperator(Criteria.where(LAST_ACCESS_TIMESTAMP).lt(date)
,Criteria.where(LAST_ACCESS_TIMESTAMP).exists(false)
);
var query = new Query();
query.limit(limit);
query.with(Sort.by(Sort.Direction.ASC, LAST_ACCESS_TIMESTAMP));
query.addCriteria(fieldsCriteria);
List<Document> list=mongoTemplate.find(query,Document.class,collectionName);
I am working on Azure Cosmos DB with SQL Api. I am using Azure SDK from:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
I have a list of ids, and I would like to find all documents with ids from my list. In order to achieve that I an using Azure SDK SqlQuerySpec, where i defined a "WHERE IN" query as follows:
List<String> ids = Lists.newArrayList("1");
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM MyLog c WHERE c.id IN (#ids)",
new SqlParameterCollection(new SqlParameter("#ids", ids)));
FeedResponse<Document> documentFeedResponse = documentClient.queryDocuments(collectionLink, spec, queryOptions);
List<Document> documents = documentFeedResponse.getQueryIterable().toList();
But unfortunately the "documents" list is empty, although in my database I have a document with id=1. Just to double check I have tried to run the query in portal:
SELECT * FROM c where c.id IN ("1")
it returns the data correctly. So I am not sure what I am doing wrong.
Anyone have already created the SqlQuerySpec in order to retrieve a list of documents with given ids?
Actually, IN is used like where c.id IN ("1"), the param is not array.
You could use Array_Contains to implement your need:
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM c WHERE array_contains(#ids, c.id )",
new SqlParameterCollection(new SqlParameter("#ids", ids)));
FeedResponse<Document> documentFeedResponse = documentClient.queryDocuments(collectionLink, spec, feedOptions);
List<Document> documents = documentFeedResponse.getQueryIterable().toList();
Output:
I have the following query and the following piece of code to get the results.
List<Map<String, Object>> rows = this.getBemsConnection().queryForList(ItemWorkflowDetails.BEMS_CREATION_DATE_QUERY, new Object[]{itemName});
if (rows != null && !rows.isEmpty()) {
for (Map<String, Object> row : rows) {
itemSetupObj.setBemsCreation((String) row.get("BEMS_CREATION"));
LOGGER.info("Bems Creation Date: {}", itemSetupObj.getBemsCreation());
}
}
String BEMS_CREATION_DATE_QUERY = "SELECT creation_date bems_creation FROM xxref_cg1_o.mtl_system_items_b WHERE segment1 = ? AND organization_id = 1";
I am getting data for this from the backend database but nothing happens when I execute the query in Java. Am I missing something?
Figured the issue, the input from Java was going in small case where as the data was stored in upper case in the table. Plus the query I ran in DB had the value in upper case. So indeed, I was not getting data as it was not able to make a match.
I am new to neo4j and graph database, and I have to send a query to get some values.
I have food and category nodes, and the relationship type between the two is specified by another node categorized_as.
What I need to fetch is the pair of food_name and its category_name.
Thanks for your help in advance.
Here's the documentation on how to run cypher queries from java. Adapted for your example, it would look like this:
// Create a new graph DB at path DB_PATH
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
// Create a new execution engine for running queries.
ExecutionEngine engine = new ExecutionEngine( db );
ExecutionResult result;
// Queries need to be run inside of transactions...
try ( Transaction ignored = db.beginTx() )
{
String query = "MATCH (f:food)-[:categorized_as]->(c:category) RETURN f.food_name as foodName, c.category_name as categoryName";
// Run that query we just defined.
result = engine.execute(query);
// Pull out the "foodNames" column from the result indicated by the query.
Iterator<String> foodNames = result.columnAs( "foodName" );
// Iterate through foodNames...
}
I have a project already developed using java,jsp and JPA (open jpa).Now i want to add a new API to retrieve data from DB.Am not much familiar with JPA.Now i want to take a join of 3 tables Atbl , Btbl, and Ctble ,then check for some conditions and finally populate bean corresopnding to table Atble.I saw a a API as follows
String sql = "SELECT A.* FROM Atble A, Btbl B WHERE A.xyz = B.pqr
AND A.field1 = ? AND B.field2 = 'SubComponent' AND B.field3 = ? ";
Query q = em.createNativeQuery(sql, A.class);
q.setParameter(1,"aa");
q.setParameter(2, "aa");
q.setParameter(3, "cc");
List<A> a = (List<A>) q
.getResultList();
Does it populate bean for A directly?If not how can i populate bean for A
This will return a List, so should work fine.
You may also consider using JPQL instead of a native SQL query, but if you are more comfortable with SQL that is fine.