0

I have a simple problem storing and retrieving an embedded collection of entity to mongo. I have checked theses question :

how to serialize class? and Mongodb saves list of object

what I understand is to save a list objects the class of that objects must extends ReflactionDBObject. This worked for saving the object, by retrieving it with the embedded collection does not work.

here a simple test show that retrieving embedded entities does not work !

@Test
  public void whatWasStoredAsEmbeddedCollectionIsRetrieved2() {

       BasicDBObject country = new BasicDBObject();

country.put("name", "Bulgaria");

List<City> cities = Lists.newArrayList(new City("Tarnovo"));

country.put("listOfCities", cities);

DBCollection collection = db().get().getCollection("test_Collection");

collection.save(country);

DBCursor object = collection.find(new BasicDBObject().append("name", "Bulgaria"));

DBObject returnedCity = object.next();

DBObject embeddedCities = (DBObject) returnedCity.get("listOfCities");

System.out.println(embeddedCities);
  }

Here is the City Class

class City extends ReflectionDBObject {

    String name;


    City() {
    }

    City(String name) {
      this.name = name;
    }

    public String getName() {
      return name;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof City)) return false;

      City city = (City) o;

      if (name != null ? !name.equals(city.name) : city.name != null) return false;

      return true;
    }

    @Override
    public int hashCode() {
      return name != null ? name.hashCode() : 0;
    }

    @Override
    public String toString() {
      return "City{" +
              "name='" + name + '\'' +
              '}';
    }

  }

The out put of the System.out.println statement is [ { "_id" : null }]

Now how can get back the embedded object and the embedded list in it ?

Community
  • 1
  • 1
Adelin
  • 18,144
  • 26
  • 115
  • 175

1 Answers1

1

If you do not have a requirement to define your own class City, you can define subdocuments using the BasicDBObjects. I only added the 'name' field to the citySubDoc1 and citySubDoc2, but of course, you can add more fields to these subdocuments.

    // Define subdocuments

    BasicDBObject citySubDoc1 = new BasicDBObject();
    citySubDoc1.put("name", "Tarnovo");    

    BasicDBObject citySubDoc2 = new BasicDBObject();
    citySubDoc2.put("name", "Sofia");

    // add to list

    List<DBObject> cities = new ArrayList <DBObject>();
    cities.add(citySubDoc1);
    cities.add(citySubDoc2);

    country.put("listOfCities", cities);

    collection.save(country);

    // Specify query condition 

    BasicDBObject criteriaQuery = new BasicDBObject();
    criteriaQuery.put("name", "Bulgaria");

   // Perform the read

    DBCursor cursor = collection.find(criteriaQuery);

    // Loop through the results
    try {
        while (cursor.hasNext()) {
           List myReturnedListOfCities = (List) cursor.next().get("listOfCities");
           System.out.println(myReturnedListOfCities);
       }
    } finally {
        cursor.close();
    } 
Kay
  • 2,942
  • 18
  • 13