/codi/mongodb/src/main/java/mongodb/ExempleProjection.java

https://gitlab.com/Dani-07/DAM-2n-POO-i-acces-a-dades · Java · 72 lines · 47 code · 16 blank · 9 comment · 1 complexity · 44a0661da834e322b8a5119919b86e09 MD5 · raw file

  1. package mongodb;
  2. import static com.mongodb.client.model.Filters.and;
  3. import static com.mongodb.client.model.Filters.eq;
  4. import static com.mongodb.client.model.Filters.gt;
  5. import java.util.concurrent.ThreadLocalRandom;
  6. import org.bson.Document;
  7. import org.bson.conversions.Bson;
  8. import com.mongodb.Block;
  9. import com.mongodb.MongoClient;
  10. import com.mongodb.client.MongoCollection;
  11. import com.mongodb.client.MongoDatabase;
  12. import com.mongodb.client.model.Projections;
  13. public class ExempleProjection {
  14. public static void main(String[] args) {
  15. MongoClient client = new MongoClient();
  16. MongoDatabase db = client.getDatabase("exemples");
  17. MongoCollection<Document> coll = db.getCollection("punts");
  18. coll.drop();
  19. for (int i=0; i<100; i++) {
  20. int x = ThreadLocalRandom.current().nextInt(100);
  21. int y = ThreadLocalRandom.current().nextInt(100);
  22. int z = ThreadLocalRandom.current().nextInt(10);
  23. Document document = new Document("x",x).append("y", y).append("z", z);
  24. coll.insertOne(document);
  25. }
  26. Bson filter = and(gt("x", 50), eq("z", 5));
  27. // Retorna tots els elements, excepte que els s'indiqui amb un 0.
  28. // db.punts.find({$and : [{"x" : {$gt:50}}, {"z" : {$eq:5}}]}, {"x":0,"_id":0})
  29. Bson projection = new Document("x", 0).append("_id", 0);
  30. coll.find(filter).projection(projection).forEach((Block<Document>) doc -> System.out.println(doc.toJson()));
  31. System.out.println();
  32. // Retorna només els elements indicats amb un 1, i l'_id.
  33. // db.punts.find({$and : [{"x" : {$gt:50}}, {"z" : {$eq:5}}]}, {"x":1})
  34. projection = new Document("x",1);
  35. coll.find(filter).projection(projection).forEach((Block<Document>) doc -> System.out.println(doc.toJson()));
  36. System.out.println();
  37. // Retorna només els elements indicats amb un 1.
  38. // db.punts.find({$and : [{"x" : {$gt:50}}, {"z" : {$eq:5}}]}, {"y":1,"_id":0})
  39. projection = new Document("y", 1).append("_id", 0);
  40. coll.find(filter).projection(projection).forEach((Block<Document>) doc -> System.out.println(doc.toJson()));
  41. System.out.println();
  42. // Equivalent al primer exemple
  43. projection = Projections.exclude("x","_id");
  44. coll.find(filter).projection(projection).forEach((Block<Document>) doc -> System.out.println(doc.toJson()));
  45. System.out.println();
  46. // Equivalent al segon exemple
  47. projection = Projections.include("x");
  48. coll.find(filter).projection(projection).forEach((Block<Document>) doc -> System.out.println(doc.toJson()));
  49. System.out.println();
  50. // Equivalent al tercer exemple
  51. projection = Projections.fields(Projections.include("y"), Projections.excludeId());
  52. coll.find(filter).projection(projection).forEach((Block<Document>) doc -> System.out.println(doc.toJson()));
  53. System.out.println();
  54. client.close();
  55. }
  56. }