Support MongoDB DbRef fields #113

Closed
rocketraman opened this Issue Mar 8, 2012 · 18 comments

Comments

Projects
None yet
6 participants
@rocketraman
Contributor

rocketraman commented Mar 8, 2012

MongoDB supports DbRef fields when one document needs to refer to another in a standard way -- see http://www.mongodb.org/display/DOCS/Database+References.

It would be really excellent if QueryDSL could support doing queries where fields that represented DbRefs were automatically traversed during a query. For example (using the Spring Data Mongodb annotations):

@Document
Obj1 {
  @DbRef Obj2 obj2Ref;
}

@Document
Obj2 {
  String foo;
}

then one could do the following query:

 query
    .where(QObj1.obj2Ref.foo.eq("Bob"))
    .list()

@ghost ghost assigned timowest Mar 9, 2012

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Mar 10, 2012

Member

This sounds like a valid addition, but might be quite difficult to implement, since dbref on database level doesn't seem to be supported.

So it's multiple nested queries or maybe map-reduce. Do you see other implementation options?

Member

timowest commented Mar 10, 2012

This sounds like a valid addition, but might be quite difficult to implement, since dbref on database level doesn't seem to be supported.

So it's multiple nested queries or maybe map-reduce. Do you see other implementation options?

@rocketraman

This comment has been minimized.

Show comment
Hide comment
@rocketraman

rocketraman Mar 11, 2012

Contributor

Agreed, I don't see any other option than nested queries or map-reduce. I think as long as it is made clear in the documentation, I don't see an issue with using multiple queries to satisfy the query DSL. I suspect multiple queries will be faster than map-reduce.

Contributor

rocketraman commented Mar 11, 2012

Agreed, I don't see any other option than nested queries or map-reduce. I think as long as it is made clear in the documentation, I don't see an issue with using multiple queries to satisfy the query DSL. I suspect multiple queries will be faster than map-reduce.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Mar 11, 2012

Member

Here I'd also prefer to apply some more explicit joining

query.join(doc.obj2Ref, obj2).on(obj2.foo.eq("Bob"))

This could be continued with a where-clause if additional constraints are needed. This separates the query layers quite clearly. Is something like this ok?

Member

timowest commented Mar 11, 2012

Here I'd also prefer to apply some more explicit joining

query.join(doc.obj2Ref, obj2).on(obj2.foo.eq("Bob"))

This could be continued with a where-clause if additional constraints are needed. This separates the query layers quite clearly. Is something like this ok?

@rocketraman

This comment has been minimized.

Show comment
Hide comment
@rocketraman

rocketraman Mar 11, 2012

Contributor

Yes, that would work nicely I think.

Contributor

rocketraman commented Mar 11, 2012

Yes, that would work nicely I think.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 3, 2012

Member

The first working version is ready. The algorithm goes through the joins from last to first and provides as an output an extended filter condition for the first query. This will probably work quite well for many-to-one kind of relations. Maybe not so well for others.

Member

timowest commented Apr 3, 2012

The first working version is ready. The algorithm goes through the joins from last to first and provides as an output an extended filter condition for the first query. This will probably work quite well for many-to-one kind of relations. Maybe not so well for others.

timowest added a commit that referenced this issue Apr 4, 2012

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 4, 2012

Member

Here are some new tests

@Test
public void Double2() {
    assertEquals("Mike", where()
            .join(user.friend(), friend).on(friend.firstName.eq("Mary"))
            .join(user.enemy(), enemy).on(enemy.firstName.eq("Ann"))
            .singleResult().getFirstName());
}    

@Test
public void Deep() {
    // Mike -> Mary -> Jane
    assertEquals("Mike", where()
            .join(user.friend(), friend).on(friend.firstName.isNotNull())
            .join(friend.friend(), friend2).on(friend2.firstName.eq("Jane"))
            .singleResult().getFirstName());
}
Member

timowest commented Apr 4, 2012

Here are some new tests

@Test
public void Double2() {
    assertEquals("Mike", where()
            .join(user.friend(), friend).on(friend.firstName.eq("Mary"))
            .join(user.enemy(), enemy).on(enemy.firstName.eq("Ann"))
            .singleResult().getFirstName());
}    

@Test
public void Deep() {
    // Mike -> Mary -> Jane
    assertEquals("Mike", where()
            .join(user.friend(), friend).on(friend.firstName.isNotNull())
            .join(friend.friend(), friend2).on(friend2.firstName.eq("Jane"))
            .singleResult().getFirstName());
}
@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 4, 2012

Member

The syntax is a little bit verbose, but the execution semantics are quite simple and transparent, so it could be released like this. Any objections?

Member

timowest commented Apr 4, 2012

The syntax is a little bit verbose, but the execution semantics are quite simple and transparent, so it could be released like this. Any objections?

@rocketraman

This comment has been minimized.

Show comment
Hide comment
@rocketraman

rocketraman Apr 6, 2012

Contributor

I haven't had a chance to try out the snapshot yet, but the tests look good.

Contributor

rocketraman commented Apr 6, 2012

I haven't had a chance to try out the snapshot yet, but the tests look good.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 20, 2012

Member

Released in 2.5.0

Member

timowest commented Apr 20, 2012

Released in 2.5.0

@timowest timowest closed this Apr 20, 2012

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest May 5, 2012

Member

@rocketraman Did you have the chance to try out the feature? Anything else that should be added for Querydsl Mongodb?

Member

timowest commented May 5, 2012

@rocketraman Did you have the chance to try out the feature? Anything else that should be added for Querydsl Mongodb?

@bananaspliff

This comment has been minimized.

Show comment
Hide comment
@bananaspliff

bananaspliff Jun 19, 2012

Hey,

i would like to do the same with list of @DBRef like this :

@DBRef
private List<FileSys> registeredChannels = new ArrayList<FileSys>();

my request > QProfile.profile.registeredChannels.contains(channel)

(channel is an FileSys object)

always return an empty list

please, help me

Hey,

i would like to do the same with list of @DBRef like this :

@DBRef
private List<FileSys> registeredChannels = new ArrayList<FileSys>();

my request > QProfile.profile.registeredChannels.contains(channel)

(channel is an FileSys object)

always return an empty list

please, help me

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Jun 20, 2012

Member

@Xavier59 Could you open a new ticket for this?

Something like "Support for DBRef List traversal in Querydsl Mongodb"

Member

timowest commented Jun 20, 2012

@Xavier59 Could you open a new ticket for this?

Something like "Support for DBRef List traversal in Querydsl Mongodb"

@rocketraman

This comment has been minimized.

Show comment
Hide comment
@rocketraman

rocketraman Sep 7, 2012

Contributor

@timowest I haven't had a chance to try out the new feature yet... however FYI the spring-data mongodb guys are thinking about how to use this from the spring-data mongodb querydsl integration. You might have some useful thoughts on it...

https://jira.springsource.org/browse/DATAMONGO-362

Contributor

rocketraman commented Sep 7, 2012

@timowest I haven't had a chance to try out the new feature yet... however FYI the spring-data mongodb guys are thinking about how to use this from the spring-data mongodb querydsl integration. You might have some useful thoughts on it...

https://jira.springsource.org/browse/DATAMONGO-362

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Sep 8, 2012

Member

Thanks, I will take a look

Member

timowest commented Sep 8, 2012

Thanks, I will take a look

@noter

This comment has been minimized.

Show comment
Hide comment
@noter

noter Sep 18, 2012

@timowest is there a possibility to construct query with OR between join predicates?

public class Car {

    BigInteger id;
    String name;

    @DBRef
    Engine engine;

    @DBRef
    Radio radio;

}

public class Radio {

    BigInteger id;
    Boolean rds;

}

public class Engine {

    BigInteger id;
    Integer power;

}

And I would like to get cars with engine more then 100 power or cars with radios with rds.

noter commented Sep 18, 2012

@timowest is there a possibility to construct query with OR between join predicates?

public class Car {

    BigInteger id;
    String name;

    @DBRef
    Engine engine;

    @DBRef
    Radio radio;

}

public class Radio {

    BigInteger id;
    Boolean rds;

}

public class Engine {

    BigInteger id;
    Integer power;

}

And I would like to get cars with engine more then 100 power or cars with radios with rds.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Sep 18, 2012

Member

@noter At the moment not.

Member

timowest commented Sep 18, 2012

@noter At the moment not.

@marskobe

This comment has been minimized.

Show comment
Hide comment
@marskobe

marskobe Aug 22, 2016

@timowest finally

query
.where(QObj1.obj2Ref.foo.eq("Bob"))
.list()

can this realized??

@timowest finally

query
.where(QObj1.obj2Ref.foo.eq("Bob"))
.list()

can this realized??

@vineey

This comment has been minimized.

Show comment
Hide comment
@vineey

vineey Sep 4, 2016

+1 looking for this easier query syntax

vineey commented Sep 4, 2016

+1 looking for this easier query syntax

@vineey vineey referenced this issue in vineey/archelix-rsql Sep 4, 2016

Open

How to query by relationship? #22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment