-
Aug 2nd, 2012, 06:21 PM
#1
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,
Asaf
-
Aug 3rd, 2012, 02:31 AM
#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.
-
Aug 3rd, 2012, 04:27 PM
#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.io.Serializable;
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) {
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<IAddress> getAddresses() {
return address;
}
}
package org.spring.mongodb.example;
import java.io.Serializable;
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) {
this.country = country;
}
public String getCountry() {
return country;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}
package org.spring.mongodb.example;
import org.springframework.data.mongodb.core.MongoOperati ons;
import org.springframework.data.mongodb.core.MongoTemplat e;
import org.springframework.data.mongodb.core.query.Criter ia;
import org.springframework.data.mongodb.core.query.Query;
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();
address1.setCity("city1");
address1.setCountry("country1");
Address address2 = new Address();
address2.setCity("city2");
address2.setCountry("country2");
person1.getAddresses().add(address1);
person2.getAddresses().add(address2);
mongoOps.insert(person1);
mongoOps.insert(person2);
Query cityQuery = new Query(Criteria.where("address:city").is("city2"));
person1 = mongoOps.findOne(cityQuery, Person.class);
System.out.println(person1);
}
}
************************************************** *
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 address.city which is city2.
For some reason (i guess because i'm doing something wrong) the person document is not found.
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules