1

I have implemented a relationship between two DBObjects using a DBRef, as described @ http://docs.mongodb.org/ecosystem/drivers/java-types/

For the sake of this question, we'll use the following two Java objects:

Foo { String name, ... }
Bar { Foo foo, ... }

The relationship works; I can see the DBRef BSON-object as an embedded document within any given instance of Bar while I'm at the MongoDB command-line, and when I query instances of Bar from Java, instances of Foo (and not DBRef) are presented as embedded documents.

My question is this: how can I search for instances of Bar which have 'foo' fields with a specific value for the 'name' field?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
djaqua
  • 11
  • 3
  • You cannot. MongoDB does not perforrm "JOINS" in any way and the general intention is to rather "embed" related data within the document in the same collection. A `DBRef` is one concept supported in some drivers where it may "magically" load that data from another collection when encountered. But this is not a "JOIN" and the data from another collection cannot be queried. Creating manual references is generally preferred logic as described in the [manual](http://docs.mongodb.org/manual/reference/database-references/). – Neil Lunn Jan 28 '15 at 00:21
  • You're right, I discovered that painfully too late. The manual reference is tedious, but worth it if you don't want a _lot_ of repetitive data. Thanks for the reply! – djaqua Feb 25 '15 at 00:47

1 Answers1

1

Your MongoDB search would look something like this, when querying the Bar collection:

{ "foo.name": "some name to search for" }

Alex
  • 2,435
  • 17
  • 18
  • The question is asking about referencing the properties of of another document via a `DBRef` when making a query. You cannot do that. – Neil Lunn Jan 28 '15 at 00:23