Mongodb GORM查询多个集合

Mongodb GORM查询多个集合,mongodb,grails,join,collections,gorm,Mongodb,Grails,Join,Collections,Gorm,我正在与Grails和MongoDB合作。我有两个域类User和AddWebsite。一个用户有许多网站,每个网站都属于一个用户。 域类如下所示: class AddWebsite{ String website User user static belongsTo = [user: User] static constraints = { website url:true user nullable:true } } 另一个域类如下所示: class User { S

我正在与Grails和MongoDB合作。我有两个域类User和AddWebsite。一个用户有许多网站,每个网站都属于一个用户。 域类如下所示:

class AddWebsite{
String website
User user
static belongsTo = [user: User]
static constraints = {
    website url:true
    user nullable:true
}
}
另一个域类如下所示:

class User {
    String login
    String password
    static hasMany = [
        addWebsites: AddWebsite
    ]
    static mapping = {
        addWebsites cascade:"all-delete-orphan"
      }
    static constraints = {
        }
    }

我需要根据当前登录用户查询AddWebsite表,并获取该特定用户的网站。有人能提出任何方法吗

我没有使用MongoDB,但由于GORM支持它,我假设一个示例查询可以满足您的需要:

AddWebsite.findallbyser(userObj)

如果您只是想要网站字符串,那么这应该可以:

def userWebsites=AddWebsite.findallbyser(userObj)*.website

其中用户网站将成为
列表

有关更多信息,请参阅


顺便说一句,您的AddWebsite类中不需要“User User”,因为您在自己的belongsTo中已命名为“User”。

我使用了这种方法。可能不是最有效的,但它很有效

You need to create a createCriteria List as follow

def c = AddWebsite.createCriteria()
def results = c.list {

           //find the user based on the relationship
             user {

                   ideq(userobj?.id.toLong())

              }

          //you can user projection here if u need a single value

    }
def showWebsites(){
    def p = User.findByLogin(session["user"].login)
    def websites = AddWebsite.findAllByUser(p['_id'])
    [websitesList: websites] 
}
在我的普惠制中,我有:

<g:select name="websiteSelection" from="${websitesList.website} " />

谢谢您的链接。它真的很有用。