Announcement Announcement Module
No announcement yet.
Trying to query for a document according to criteria on array Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to query for a document according to criteria on array

    Hi Ladies and Gents,
    I am trying to query for a document 'Person' which holds an array of 'Address' object where Address field called 'City' is named 'abc'.

    I have been trying tediously to query for this information with not success.

    Any help will be highly appreciated.

    Thank you,

  • #2
    It's helpful to list what you've tried already and skim through the JavaDocs a bit. What exactly didn't work? No one will give you a 1:1 on using the API here.


    • #3
      Let me add some additional information...

      Here is some code for a test project I put together:

      package org.spring.mongodb.example;

      import java.util.ArrayList;
      import java.util.List;

      public class Person implements Serializable {

      private static final long serialVersionUID = -5129190663156726494L;
      private String id;
      private String name;
      private List<IAddress> address = new ArrayList<IAddress>();

      public Person() {
      public Person(String name) { = name;
      public String getId() {
      return id;
      public String getName() {
      return name;
      public void setName(String name) { = name;
      public List<IAddress> getAddresses() {
      return address;

      package org.spring.mongodb.example;

      public interface IAddress extends Serializable {
      public abstract void setCountry(String country);
      public abstract String getCountry();
      public abstract void setCity(String city);
      public abstract String getCity();

      package org.spring.mongodb.example;

      public class Address implements IAddress {

      private static final long serialVersionUID = -861541388927332292L;
      private String country;
      private String city;
      public Address(){
      public void setCountry(String country) { = country;
      public String getCountry() {
      return country;
      public void setCity(String city) { = city;
      public String getCity() {
      return city;

      package org.spring.mongodb.example;

      import ons;
      import e;
      import ia;
      import com.mongodb.Mongo;

      public class MongoApp {
      public static void main(String[] args) throws Exception {

      Mongo mongo = new Mongo();
      MongoOperations mongoOps = new MongoTemplate(mongo, "person");

      Person person1 = new Person("person1");
      Person person2 = new Person("person2");

      Address address1 = new Address();

      Address address2 = new Address();



      Query cityQuery = new Query(Criteria.where("address:city").is("city2"));
      person1 = mongoOps.findOne(cityQuery, Person.class);

      ************************************************** *
      So... what you can see here, are three classes.
      IAddress - a very simple interface holding city and country getters
      Address - an implementation of IAddress
      Person - A simple class holding among other things a list of IAddress.
      MongoApp - the main class. On this i've created the classes, inserted them into mongo and then trying to find a person who has an which is city2.

      For some reason (i guess because i'm doing something wrong) the person document is not found.

      Thank you