I have first expirence in grails+mongodb. And I have a problem on saving object. First I connected local mongo db in such way to grails:
grails {
mongo {
host = "localhost"
//port = 27107
//username = "login"
//password="pwd"
databaseName = "db"
}
}
I don't know why, but if I specify port and login+password (I create such user with such password), grails give me an error, that cann't connect to mongodb. This is a log of mongodb on daemon start:
22:47:04 [initandlisten] MongoDB starting : pid=918 port=27017 dbpath=/var/lib/mongodb 64-bit host=ubnt-VirtualBox
Ok, with out port and login+pass it works. Next I create domain:
class Cover {
String name
String url
static constraints = {
name(blank: false)
url(blank: false)
}
static mapping = {
collection "cover"
database "covers"
}
}
I try to save it:
Cover cover = new Cover()
cover.name = title.text()
cover.url = url
println("Try to save object: ${cover.toString()}")
cover.save()
println("After save object: ${cover.toString()}")
What I have in output:
Try to save object: com.mydomain.Cover : (unsaved)
After save object: com.mydomain.Cover : 23
When I run shell with command 'mongo', then try to:
>use covers
>db.cover.find()
I see that there is nothing in collection. But when I look at db.cover.next_id.find() I see 23. So it seems like id increments. I can't figure out why object didn't save to mongodb. Why? Before I try to connect to mongodb and save via java driver and saving works (so mongodb seems to install correctly).
Also when I run shell I see such info >connecting to: test what means test? Where I could configure it before? May be shell connects to one db and grails to another?
For me it seems it's saving without problems unless you've some constraint errors...
Have you tried to print the errors after saving/validating?
println cover.errors
Also you could try to recover all the instances from the application without looking in mongo with
println Cover.list()
If with this last line you can see your saved instances, maybe grails is mixing the databases as you have in databaseName=db and you later say that Cover should be saved to covers database
I have found an error, it discovers for me that source in src/groovy have to access to grails sources in specific way, so to save cover I need to create service in grails, where I cna implemet save method, and they in place where I want to call save I should get service in such way
def ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
CoverService coverService = ctx.coverService
And now it works.
Related
Im on a mac(OS 10.14) using nodejs 14 and PostgresSQL 12.
I just installed Loopback4 and after following this tutorial Im not able to use any of the enpoints that use Models, ie that connect to Postgres, I constantly get a timeout.
It seems like its not even reaching the Postgres Server, but the error gives no information, just that the request times out.
There are no issues with the Postgres server since I can connect and request information with other nodejs applications to the same database.
I also tried to set this as the host host: '/var/run/postgresql/', same result.
I now tried the approach with a Docker container, setting the datasource files as follows:
import {inject, lifeCycleObserver, LifeCycleObserver} from '#loopback/core';
import {juggler} from '#loopback/repository';
const config = {
name: 'mydb',
connector: 'postgresql',
url: 'postgres://postgres:mysecretpassword#localhost:5434/test',
ssl: false,
};
// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
#lifeCycleObserver('datasource')
export class PostgresSqlDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'PostgresSQL';
static readonly defaultConfig = config;
constructor(
#inject('datasources.config.PostgresSQL', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
With that same url I can log on my command line from my mac.
Is there a way to add logging and print any connection error? Other ways to debug it?
[UPDATE]
As of today Loopback4 Postgres connector does not work properly with Nodejs 14.
When starting the application, instead of running
npm start, you can set the debug string by running:
DEBUG=loopback:connector:postgresql npm start
If you want it to be more generic, you can use:
DEBUG=loopback:* npm start
I am trying to save a root Object (MyDocument) into a mongoDB with authentication enabled and a ReplicaSet consisting of 3 Nodes (as inserted into mongoUrls)
With this call:
(VOMongoRepository
mongoUrls: {'127.0.0.1:27017' . '127.0.0.1:27018' . '127.0.0.1:27019'}
database: 'myDB'
username: 'myUser'
password: 'myPass') enableReplication
I receive a VOMongoConnectionError without any deeper information.
Trying the same with this:
VOMongoRepository
mongoUrls: {'myUser:myPass#127.0.0.1:27017/?replicaSet=myRepl' }
database: 'myDB'
I then receive a VOMongoError "not authorized for Query on myDB.MyDocument"
The credentials are double checked with mongo client and Compass, also the read/write permissions (actually the role is dbOwner).
Interestingly my testDocumentLifeCycle is able to create the object and to send a message to save, that returns without signaling an error, although it does not create the document in MongoDB. But the selectOne: is then returning the VOMongoError:
| doc |
MyDocument new
identity: 'me#there.com';
save.
user := MyDocument selectOne: [ :each | each identity = 'me#there.com'].
Just to mention: the above test for MyDocument class did work with a standalone mongod without authentication enabled. The only thing changed is the repository.
So what am I doing wrong?
Actually there is a bug in the replicaSet part of VoyageMongo. It is not using the credentials provided. It has been posted at https://github.com/pharo-nosql/voyage/issues/104
I am having an issue where I need to manually map the correct database to the domain instead of it being picked up from the connection argument.
I am using grails 3.2.8, plugin "org.grails.plugins:mongodb:6.1.0". I have both hibernate and mongodb plugin enabled.
I have define my connection URL as
//application.yml
mongodb:
url: 'mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}#${MONGODB_REPLICA_SET}/${MONGODB_DATABASE}?${MONGODB_CONNECTION_OPTIONS}'
My domain object is defined as :
class ReportData {
String id
Long someField
static mapWith = "mongo"
static mapping = {
//database "db-name" DOESN'T WORK WHEN COMMENTING OUT THIS LINE
}
}
Shouldn't the database(system property MONGODB_DATABASE) be picked up auto-magically from the connection url? I am not sure if this is a bug or I am missing some configuration aspect.
I realized that I had not added following in my build.gradle file:
bootRun {
systemProperties = System.properties
}
so my application environment settings were not even getting applied correctly and hence my connection url was invalid.
I found that detail here: http://docs.grails.org/latest/guide/conf.html
I have successfully added mongodb to my server and I am able to work from the mongo shell no problem, I can also connect to the database from php just fine. I have also downloaded and semi successfully installed the plugin for mongodb and cakephp. However I am now stuck with cakephp not able to connect to the database. I have followed both ichikawa's github and mark story's web page on the subject but neither has helped get over this hump. Does anyone have any suggestions on what I should try to get this hooked up and running?
I ran into this and a buddy found a link that told us to replace line 185 of /app/Plugin/Mongodb/Model/Datasource/MongodbSource.php. Here is the old line and the new line.
// $this->connection = new Mongo($host, array("persist" => $this->config['persistent']));
$this->connection = new Mongo($host);
I can try to locate the URL but the solution was above.
Because your server using mongodb driver 1.x. Let's fix:
In app/Plugin/Monggodb/MongodbSource.php (line 197), find:
else if ($this->_driverVersion >= '1.3.0') {
$this->connection = new $class($host); // mongodb 2.x
}
replace:
else if ($this->_driverVersion >= '1.3.0' && $this->_driverVersion < '2.0') {
$this->connection = new $class("mongodb://loginID:password#IP"); // mongodb 1.5.6, loginID: your mongodb user login;
}
With above code, you changed connect string, because connect string had different between mongo 2.x and 1.x
Vote if it's right! Fun!
Quite simply, I'm trying to connect to a MongoDB via Node.js:
Db = require('../v2/node_modules/mongodb').Db
Connection = require('../v2/node_modules/mongodb').Connection
Server = require('../v2/node_modules/mongodb').Server
console.log "before"
DbServer = new Server("localhost", 27017, {})
db = new Db("twitter", DbServer, {native_parser:true})
console.log "after"
return
That's my code and it's as simple as it gets. My output, however, seems to stop at the db = new Db... line.
It never gets to the after. It doesn't give an error either. I know I have a DB running and when I fire up MongoHub, it's there along with the twitter database
Just remove the native_parser=true would be ok
Native bson parser not compiled, please compile or avoid using native_parser=true