I am pretty new to mongo db, and I have a simple question regarding a trouble I can’t solve in my Java program (3.0.2 client version). My aim is to perform a distinct on the “cars” test database, and I am trying this code:
DistinctIterable<Object> classification = collection.distinct("classification", null);
I can’t figure out what should I put in the second parameter. Could you help me please?
If you are using the Java API, I think that you can pass just the first argument, that would be the one on which do the distinct. The second parameter would be the query to filter on, but it can be omitted according to the documentation.
public List distinct(String fieldName)
Find the distinct values for a specified field across a collection and returns the results in an array.
Parameters:
fieldName - Specifies the field for which to return the distinct values.
Returns:
a List of the distinct values
You need to provide the class to map to, see http://api.mongodb.org/java/current/com/mongodb/client/MongoCollection.html
Related
I am not sure if that is possible or not and after a lot of research I ended up here to ask for your help or even guidance.
So, let's say I have a json array that has 10 different types of objects inside the array. This is a json that is being retrieved through an API with sports games.
What I need to do is filtering through these objects in my application. I am using JAVA and so far I have ended up that I will use stream filter and predicates. I am aware that I can create different types of predicates and put them in the stream.filter() function, but is it possible to do it somehow dynamically?
For example, I need to filter this array by time. This predicate will be
return p -> p.getTime() > 1;
And then:
return match.stream().filter( predicate ).collect(Collectors.<Match>toList());
What if another filter has another one condition which is team name. Is it possible to add some how the other predicate and also add the "AND" "OR" condition between those two? I need to do this dynamically using one filter function with different predicates.
Is there a way to make something like a custom query to store it in a database and retrieve it and use it like a predicate? Or the predicate itself is it possible to be stored in a database?
If I am completely wrong on this please guide me to find another way to do this. Otherwise a help would be appreciated. Thank you and happy new year to all. :)
This is an interesting problem. And I think this will not be uncommon face as well considering data lake scenarios.
I think, as suggested in a comment above, the way to apply is to have a Predicate. You may have a predicate that applies the conditions as AND or OR and then supply it to the stream processor. Like this (assuming that you have a base class Data to which you have mapped your API output):
/* Create the predicate with the conditions. Showing 2 here with an "AND" combination. */
Predicate<? extends Data> p = d -> d.getTime() > 1;
p.and( d -> d.getName().equals( "Football" ) ); //Consider ".or()" here, if that is what you need.
/* Supply this predicate to the stream processor. */
match.stream().filter( p ).collect(Collectors.<Match>toList());
Using an and() call is the same as calling .filter() one after the other on the stream processor. Something like this:
stream.filter(...).filter(...)...
So, you will be able to construct such a stream call in a for loop.
Is there a way to make something like a custom query to store it in a database and retrieve it and use it like a predicate? Or the predicate itself is it possible to be stored in a database?
You may do this within your Predicate itself. That is, instead writing the logic as shown above, you may make a database call to fetch you Java code. However, you will have to do dynamic compilation using JavaCompiler. That may be a bit complicated. However, you may consider a JVM-based scripting language like Groovy for such things.
In this answer, the author mentions that to avoid NPE the fetchValue(query) method can be used. The problem is that how exactly can the OP's code be converted into a query? I have similar code, pasted below, and would like to turn it into a query also.
return jooqDSLContext.select()
.from(CL_LOGIN)
.join(CL_USERS)
.on(CL_LOGIN.CL_USER_ID.eq(CL_USERS.CL_USER_ID))
.where(CL_USERS.EMAIL1.eq(email))
.fetchOne().into(CL_LOGIN);
JOOQ is very powerful and has many capabilities, but unfortunately everything I have tried to make a standalone query object with a join does not even compile.
EDIT: The answer provided did help me side-step the need to have a query object. But for those that want to know how to get a query object you can use the getQuery() method... see example below.
SelectQuery<Record1<String>> query = jooqDSLContext.select(USER_LOGIN.ACCOUNT_STATUS)
.from(USER_LOGIN)
.where(USER_LOGIN.USER_ID.eq(userId))
.getQuery();
Observe the signature of the method DSLContext.fetchValue(ResultQuery<R>), where R extends Record1<T>. This means that the expected row type of the query is Record1<T> with any arbitrary <T> type. In other words, you must project exactly one column in your SELECT clause.
You seem to want to project the entire record of type CL_LOGIN, so fetchValue() is not applicable to your use-case.
But note, there's also ResultQuery.fetchOneInto(Table), which is a convenience method wrapping that null check and the into() call. So, just write:
return jooqDSLContext.select()
.from(CL_LOGIN)
.join(CL_USERS)
.on(CL_LOGIN.CL_USER_ID.eq(CL_USERS.CL_USER_ID))
.where(CL_USERS.EMAIL1.eq(email))
.fetchOneInto(CL_LOGIN);
Suppose I want I have a SQL like select * from game;, but I want only one SQL to execute to group different games result into Java List.
Say we have game1, game2, game3 in the content of column 'game' in the resultset, so I need a List whose length is 3 and each element inside is a SQL ResultSet(if I can have a OR Mapping, that will be great) so that I can parse them later.
SpringFramework contains a class called JdbcTemplate which contains a method called query, this is will take a SQL statement with relevant parameters and return a List with your results but it will require some work to set it up. I think you're looking for something a bit more 'out of the box' which just does it in a single line, but I don't think this is possible. There are other methods in the class which do the same thing but take different parameters based on what you have available in your code.
See the JdbcTemplate api for more details if you're interested.
I have a class that populates a Hibernate SQL query's parameters with different values depending on some input criteria. I would like to write tests for the resulting query. As of yet, I have not found a way to get the parameter values that were set on a query. I looked at the resulting Query implementation (in this case, SQLQueryImpl) and there is a method called getParameterMetadata() that seems like it might give me something, but I haven't been able to find anything.
I would like to be able to do something like:
assertEquals("some value", query.getParameterValue(parameterName));
Is there a way to do this?
EDIT:
I just looked a little more, and I found a
query.getQueryParameters(map).getNamedParameters()
which returns a Map. I have to assume that the map contains the parameters and their values. Correct me if I'm wrong.
I actually didn't consider the fact that because I'm testing, I can simply mock the query and expect specific parameters.
I am using eclipse Tools to generate my Annotated Domain Code Classes.
For the One to Many & Many to Many Relationships, the code generated
used Set type for collections.
I want to change it to List or ArrayList. What should be my configuration
in reveng.xml
Also, what are the standard conversion types between MySQL and Java.
I mean like varchar is converted to string, int to int etc.
Can anyone share a pretty much standard reveng.xml file for type conversions...???
You shouldn't use List by default instead of Set. But if you need it punctually, that can help you:
public <T> List<T> fromSetToList(Set<T> set) {
return new ArrayList<T>(set);
}
Also, what are the standard conversion types between MySQL and Java. I mean like varchar is converted to string, int to int etc.
For reference on Hibernate mappings, I found the following link helpful for basic scenarios. For more complex mappings, refer to the full hibernate documentation.
Hibernate Mapping Cheat Sheet
As for The List vs. Set, Set should actually be the Collection type you should use. The only difference between List and Set is that List implies order of the elements and Set does not allow duplicates. A simple DB record set does not have a specified order and it does not have duplicates, so a Set is appropriate. A List would be useful only if your query did specify order and/or you wanted some kind of UNION which may produce duplicates.
I don't know how to turn your Sets into Lists but I would encourage you to question if you actually want to do so.