如何从groovy / grails查询mongodb? [英] How to query mongodb from groovy/grails?

查看:192
本文介绍了如何从groovy / grails查询mongodb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须有一个域对象来查询 mongodb 吗?

如果我只想显示一些原始数据,该怎么办?从我的控制器中查询 mongodb 的语法是什么?



我试过了

 def var = db.nameOfMyCollection.find()

但它在我的控制器类中没有说db等属性。

我知道我的应用程序正在连接到数据库,因为我正在监视 mongo server log并且它增加了数字当我启动我的grails应用程序时,它会连接一个。解析方案

假设你在build config中添加了mongodb java驱动程序依赖项,并刷新了你的



创建一个名为 MongoService.groovy 的grails服务并放入以下代码。



不要忘记导入mongodb

  package com.organisation.project 

import com.mongodb 。*


class MongoService {
private static MongoClient mongoClient
private static host =localhost//您的主机名
private static port = 27017 //你的港口号码
private static databaseName =your-mongo-db-name

public static MongoClient client(){$ b $ if(mongoClient == null){
return new MongoClient(主机,端口)
} else {
返回mongoClient
}
}

public DBCollection集合(collectionName){
DB db = client().getDB(databaseName)
return db.getCollection(collectionName)
}
}

我们现在可以在我们的控制器或其他服务中使用这个MongoService。



现在,您可以在控制器中执行以下操作。



不要忘记导入mongodb.DBCursor

  package com.organisation.project 



import com.mongodb.DBCursor

class YourControllerOrService {

def mongoService //包括Mongo服务

def method(){
def collection = mongoService.collection( ())
DBCursor cursor = collection.find()
尝试{
while(cursor.hasNext()){
def doc = cursor.next()
println doc //会在您的数据库中打印原始数据
}

} finally {
cursor.close()
}





$ p $更多信息请参阅 mongodb java文档


Do I have to have a domain object to query mongodb?

What if I just want some raw data to be displayed? What would be the syntax to query mongodb from my controller?

I tried

"def var = db.nameOfMyCollection.find()"

but it says no such property as db in my controller class.

I know that my application is connecting to the database because I am monitoring mongo server log and it increases the number of connections by one when I launch my grails app.

解决方案

Assuming you have added mongodb java driver dependency in build config and refreshed your dependencies.

Create a grails service named MongoService.groovy and put the following code.

Dont forget to import mongodb

package com.organisation.project

import com.mongodb.*


class MongoService {
    private static MongoClient mongoClient
    private static host = "localhost"    //your host name
    private static port = 27017      //your port no.
    private static databaseName = "your-mongo-db-name"

    public static MongoClient client() {
        if(mongoClient == null){
            return new MongoClient(host,port)
        }else {
            return mongoClient
        }
    }

    public DBCollection collection(collectionName) {
        DB db = client().getDB(databaseName)
        return db.getCollection(collectionName)
    }
}

We can now use this MongoService in our controllers or other services.

Now you can do following stuff in your controller.

Dont forget to import mongodb.DBCursor

package com.organisation.project



import com.mongodb.DBCursor

class YourControllerOrService {

    def mongoService    //including Mongo service 

    def method(){
        def collection = mongoService.collection("your-collection-name")
        DBCursor cursor =  collection.find()
        try{
            while(cursor.hasNext()){
                def doc = cursor.next()
                println doc     //will print raw data if its in your database for that collection
                }

         }finally {
                  cursor.close()
         }

    }
}

For more info Refer mongodb java docs

这篇关于如何从groovy / grails查询mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆