-
Notifications
You must be signed in to change notification settings - Fork 0
/
Saturday Assignment
51 lines (51 loc) · 1.88 KB
/
Saturday Assignment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
public class add {
public static void main(String[] args) {
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase database = mongoClient.getDatabase("myDb");
MongoCollection<Document> collection = database.getCollection("employee");
collection.drop();
List<Document> employees = new ArrayList<>();
employees.add(new Document("name", "John")
.append("salary", 15000)
.append("age", 28));
employees.add(new Document("name", "Alice")
.append("salary", 35000)
.append("age", 32));
employees.add(new Document("name", "Bob")
.append("salary", 20000)
.append("age", 29));
employees.add(new Document("name", "Carol")
.append("salary", 18000)
.append("age", 38));
employees.add(new Document("name", "David")
.append("salary", 12000)
.append("age", 42));
employees.add(new Document("name", "Eva")
.append("salary", 40000)
.append("age", 36));
collection.insertMany(employees);
System.out.println("Employees inserted.");
// Query for employees with age between 30 and 40, sorted by salary ascending, and limit to one result
Document query = new Document("age", new Document("$gte", 30).append("$lte", 40));
FindIterable<Document> lowSalaryEmployee = collection.find(query)
.sort(Sorts.ascending("salary"))
.limit(1);
System.out.println("Employee aged between 30 and 40 with the lowest salary:");
for (Document emp : lowSalaryEmployee) {
String name = emp.getString("name");
Integer salary = emp.getInteger("salary");
Integer age = emp.getInteger("age");
System.out.println("Name: " + name + ", Salary: " + salary + ", Age: " + age);
}
}
}