MongoDB/Java: most efficient way to get values from query [duplicate] - java

This question already has answers here:
Mongodb Java - How to return restricted fields with find() or findOne()
(4 answers)
extracting values from HashMap
(5 answers)
Closed 3 years ago.
My MongoDB documents look something like this:
{
"person_id": 12345,
"first_name": "John",
"last_name": "Doe",
...
}
I now want a query that gives me a Collection<Long> of person_ids, with some filters applied. Filtering works fine, but MongoCollection.find returns a MongoCollection of Documents.
Is there an implicit way to just get the values (12345), not key-value-pairs ("person_id" : 12345)?
Right now I'm just filling a new Collection by iterating over the result, extracting the values one by one. If there's no other way to do it, is there any point in using a projection to restrict the returned fields to person_id, or is that just overhead?

Related

How to use Java Streams to extract Specific Data Into Hashmap [duplicate]

This question already has answers here:
How can I count occurrences with groupBy?
(6 answers)
Counting occurrences in a list with Java 8 [duplicate]
(1 answer)
Closed 1 year ago.
I have a record for Employees:
public record Employee(
String name,
String id,
string imm_supervisor,
){}
I have a stream of Employee objects, and I want to extract a hashmap that gives me a mapping between each imm_supervisor and the amount of employees that the supervisor directly supervises.
I'm frankly confused by documentation, and I've been getting errors not knowing how stream this properly. This is as close as I got:
Map<String, Integer> mp = streamWithEmployees.map(x->supervisorofEmployee(x)).collect(groupingBy(/*String*/, counting()));
supervisorofEmployee(employee) returns a string, imm_supervisor. I'm not sure how to grab the returned strings from the map and put them into the map.
Hope this makes sense.

Java 2D array can't access row column name [duplicate]

This question already has answers here:
Java associative-array
(15 answers)
Closed 2 years ago.
I'm beginner in Java environment, accustomed to PHP.
And in Php we can create an array like the code below :
$array['params1'] = 'the first param';
$array['params2'] = 'the second param';
And when i will output $array['params1'] it will be 'the first param'.
But i do not find any similar solutions in Java, do you know something similar ?
Thanks in advance
As #mrblewog said, you might want to readup on data structures and the syntax in Java as it is quite different than php.
To give you an Example:
// Key Value
HashMap<String, String> map = new HashMap<>();
map.insert("key1", "value1");
map.get("key1"); // returns "value1"
If you want to store other objects than Strings you will need to change the generic types (written in the <X, Y>).

Passing variable into IN clause in java [duplicate]

This question already has answers here:
Hibernate Criterion IN Clause 1000 break up
(3 answers)
Java Oracle exception - "maximum number of expressions in a list is 1000"
(7 answers)
Closed 5 years ago.
I am constructing a hibernate query and I pass a list of String values into the IN clause. That list sometimes happens to be more than 1000 values so I receive an error. I have looked up some solutions like breaking that clause into smaller ones or making temporary table, but none of them showed how actually it is better to work with variable list.
So if I had something like:
SELECT * FROM MY_TABLE WHERE NAME IN (list).
What would be the best way to handle this?

org.hibernate.Criteria.setFetchSize() doesn't work [duplicate]

This question already has answers here:
What is difference between setMaxResults and setFetchSize in org.hibernate.Query?
(3 answers)
Closed 7 years ago.
I have the below code and trying to limit the number of records retrieved. (LIMIT), but i always get all the rows. I see forums recommending me to use Query, but is not possible to achieve it using criteria ?
Criteria criteria = session.createCriteria(Application.class)
.add(Restrictions.gt("lastModifiedOn", applicationLastRunTime))
.add(Restrictions.eq("lead", false))
.addOrder(Order.asc("lastModifiedOn"));
criteria.setFetchSize(40);
criteria.list()
try to use:
public Criteria setMaxResults(int maxResults)
Set a limit upon the number of objects to be retrieved.
Parameters:
maxResults - the maximum number of results
Returns:
this (for method chaining)
Criteria criteria = session.createCriteria(Application.class)
.add(Restrictions.gt("lastModifiedOn", applicationLastRunTime))
.add(Restrictions.eq("lead", false))
.addOrder(Order.asc("lastModifiedOn"));
criteria.setMaxResults(40);
criteria.list()
more info: What is difference between setMaxResults and setFetchSize in org.hibernate.Query?

How can I avoid duplicates in my "mysql" database when using hibernate (JAVA)? [duplicate]

This question already has answers here:
Best way to avoid duplicate entry into mysql database
(4 answers)
Closed 7 years ago.
I parse a .lst file to get data, then I use hibernate in order to add these data in a mysql database.
My file is always updated with new data but it KEEPS existing ones.
The question is how can I avoid duplicates data in my database when I parse the file a scond time.
Thanks
INSERT INTO table_name ('your fields') VALUES ('New values') ON DUPLICATE KEY UPDATE field1=' value',field2='value',.....;
Table Structure:
make this 2 fields (name and city )Uniqe
ex:
INSERT INTO tbl_stud(name,city)values('Divyesh','Ahmedabad') ON DUPLICATE KEY UPDATE name='Divyesh',city='Ahmedabad';

Categories

Resources