0

I have two collections, cars and car_colors, where a "car" hasA "color":

// Groovy pseudo-code
@Entity
class Car extends AbstractMongoEntity {
    String make
    String model
    CarColor color
}

@Entity
class CarColor extends AbstractMongoEntity {
    String name           // e.g. "Red"
    String label          // e.g. "RED"
    String description    // e.g. "The color red."
}

I am using Morphia 1.0.1 to OR/map between these POJO entities and MongoDB data. I am looking for a query/selector/find that will allow me to lookup all the Cars with a CarColor#label of, say, 'BLUE'. I believe this involves a JOIN operation somehow, though I'm not seeing how. Here's my best attempt so far which doesn't work:

datastore.find(Car).field('color').field('label').equal('BLUE').asList()

Any ideas?

smeeb
  • 27,777
  • 57
  • 250
  • 447
  • 1
    MongoDB is NoSQL database, which means it does not support joins. AFAIK, morphia is just a pretty wrapper over the mongo java driver, which converts documents to the corresponding objects. – Nayanjyoti Deka Sep 28 '15 at 10:21
  • Thank you @NayanjyotiDeka (+1) - yes I guess I mean "join" in a general sense, implying an association between the two collections. At the end of the day, I just need to know how to filter Cars by CarColor. Thanks again! – smeeb Sep 28 '15 at 10:23
  • Check this out, this might give you some idea how to achieve that. http://stackoverflow.com/a/22739813/2776345 . Basically for each record you are finding the corresponding object making a query to the dbcollection. Mongo driver for java has option for .forEach() as well. http://docs.mongodb.org/getting-started/java/query/ – Nayanjyoti Deka Sep 28 '15 at 10:29

1 Answers1

0

Just use ".", for example, datastore.find(Car).field('color.label').equal('BLUE').asList()

Alex P
  • 1
  • 1