Reputation: 33
I'm in front of a Mongo DB that uses lists of nested DBREF
s. (Yes, i know that's a very strange way to use Mongo, and beside this, it would be better not to use the @DBREF
)
We have collections of readers, books and authors.
The reader looks like this:
{
"_id" : ObjectId("12345"),
"age" : 37,
"books" : [
{
"$ref" : "book",
"$id" : "123"
},
{
"$ref" : "book",
"$id" : "456"
}
]
}
The $ref
contains the collection name, the $id
the id of the book.
Obviously, the books look the same way; instead of a list of books DBREFs, they have a list of authors DBREFs.
Two questions:
Trying to solve this with Java...
Upvotes: 2
Views: 644
Reputation: 34581
To query based on database references with the Java driver, use the class DBRef.
Yes. Get all book IDs of author1
with a separate query and then find all readers that have a book reference $in
your list of book IDs.
This time, you'll need three queries:
$in
your list from step 1.$in
your list from step 2.This may be quite heavy, but that's what you get for storing data in a normalized way in a database that does not support joins. Another approach would be to restructure your data in a way that will simplify your queries.
Upvotes: 1