poiati / gmongo Goto Github PK
View Code? Open in Web Editor NEWA Groovy wrapper to the mongodb Java driver
Home Page: http://blog.paulopoiati.com/2010/06/20/gmongo-0-5-released/
License: Apache License 2.0
A Groovy wrapper to the mongodb Java driver
Home Page: http://blog.paulopoiati.com/2010/06/20/gmongo-0-5-released/
License: Apache License 2.0
Can we get support for db.collection.bulkWrite()
Hello Paulo,
I have been looking at your documentation on Github and on your site. I can't seem to find three features, pagination, sorting and find by id.
I have been able to get pagination to work by following the Java Driver syntax. However, I have not been able to get find by id to work. I tried db.collection.findOne(_id: id) but this does not return any thing. In addition, I tried to use the Java syntax db.collection.findOne( "_id", new ObjectId(id)) but that didn't work either.
Would it be possible to inject "findById" to the collection object?
Also, would it be possible to have a simpler api for paginating and sorting? I don't mind trying to hack something out and submit a patch. I was thinking of something like this db.collection.find([skip:0, limit:10, sort: "asc", orderBy: "lastName"]).
Thoughts?
See http://jira.grails.org/browse/GPMONGODB-88
We are now using DBRefs to store relationships in GORM for Mongo but this is causing MissingPropertyException errors when using GMongo
Need for use MongoClient instead of Mongo class because of replicaset connection properties, such read preferences.
Hi,
thanks for the great lib. But how can multiple documents be updated with a single update?
http://docs.mongodb.org/manual/applications/update/#update-multiple-documents
Hi,
I wanted to update something by id with gmongo. I have tried:
db.song.update([_id: "511048380364c13f5505c036"], [$set:[title: "GGGGGG"]])
It works when using another field as the key for updating, for example "title". What am I missing?
Thx /Lasse
An UnsupportedOperationException is thrown when using a SingletonList in an '$in' query when using the Grails Mongo Gorm plugin. Grails has this odd behavior where you get a SingletonList when converting an http parameter to a list (params.list('userIds')) on a one item parameter. In the cases where you have multiple items in the parameter you get an ArrayList, which works fine with gmongo. The work around is to use a mutable list or cast the value to a string array.
Here is an example of a Mongo Gorm call that would fail if the collection is a SingletonList
User.collection.find([_id: ['$in': singletonListOfIds]])
java.lang.UnsupportedOperationException
at java.util.AbstractList.set(AbstractList.java:132)
at com.gmongo.internal.Patcher$__converAllCharSeqToString_closure2_closure4.doCall(Patcher.groovy:89)
at com.gmongo.internal.Patcher$__converAllCharSeqToString_closure2.doCall(Patcher.groovy:87)
at com.gmongo.internal.Patcher._converAllCharSeqToString(Patcher.groovy:84)
at com.gmongo.internal.Patcher$__converAllCharSeqToString_closure2.doCall(Patcher.groovy:102)
at com.gmongo.internal.Patcher._converAllCharSeqToString(Patcher.groovy:84)
at com.gmongo.internal.Patcher$__converAllCharSeqToString_closure2_closure4.doCall(Patcher.groovy:93)
at com.gmongo.internal.Patcher$__converAllCharSeqToString_closure2.doCall(Patcher.groovy:87)
at com.gmongo.internal.Patcher._converAllCharSeqToString(Patcher.groovy:84)
at com.gmongo.internal.Patcher$__converAllCharSeqToString_closure2.doCall(Patcher.groovy:102)
at com.gmongo.internal.Patcher._converAllCharSeqToString(Patcher.groovy:84)
at com.gmongo.internal.Patcher._convert(Patcher.groovy:73)
at com.gmongo.internal.Patcher._convert(Patcher.groovy:69)
at com.gmongo.internal.Patcher$__patchInternal_closure1.doCall(Patcher.groovy:32)
It's desirable for domain classes to have synthetic, calculated properties. Ideally these synthetic properties will not be persisted to mongodb.
class Person {
def firstName
def lastName
@transient
def getFullName {
"$firstName $lastName"
}
}
Without support for @transient a fullName field is persisted to MongodB.
@transient would also allow read-only properties and support for @lazy.
When I try to compile my groovy module with Maven using Java 8 and the new gmaven plugin with invokedynamic, my tests fails because GMongo cannot convert maps and list containing teh requests to DbObject.
Thus I am forced to disable invokeDynamic
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<configuration>
<invokeDynamic>false</invokeDynamic>
</configuration>
</plugin>
If I enable it, I have the following error:
Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.mongodb.DBObject
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:219)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
at com.mongodb.DBCollection.insert(DBCollection.java:148)
at com.mongodb.DBCollection.insert(DBCollection.java:133)
at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterf
I am using the Groovy 2.2.1 indy jar.
When performing a find using Grails integration with mongo DB plugin, we are converting a map to a basic DB object using code similar to:
DomainClass.collection.find([name:"..."] as BasicDBObject)
This appears to be going through the Patcher.groovy file in gmongo, which is having a null exception when one or some of the property values are null in the map to be converted. This seems to trace to line 114 of Patcher, where the class of each "argument" is retrieved. Output from an integration test:
N/A
java.lang.NullPointerException
at com.gmongo.internal.Patcher._types(Patcher.groovy:114)
at com.gmongo.internal.Patcher._patchInternal_closure1(Patcher.groovy:33)
at com.ace.mws.reports.Something.execute(Something.groovy:33)
at com.ace.mws.reports.SomethingIntegrationSpec.Feature here(SomethingIntegrationSpec.groovy:22)
I'm not sure of the args property in the patcher, but can we use a null-safe groovy operator here to prevent problems? We are on grails 1.3.7 which means we use gmongo 0.9.1 as far as I can tell.
I'll submit a pull request, and let me know what you think. Thanks, Brian
The error is:
java.lang.NoClassDefFoundError: org/codehaus/groovy/runtime/BytecodeInterface8
at com.gmongo.internal.Patcher._convert(Patcher.groovy:64)
at com.gmongo.internal.Patcher._patchInternal_closure1(Patcher.groovy:32)
If you compile with Groovy 1.7 then it should be able to run in both 1.7 and 1.8 but if you compile with 1.8 it won't run in Groovy 1.7.
Could gmongo be updated to compile with 1.7 please?
I apologise for asking what might seem like two silly questions but I can't find this information elsewhere.
Background:
I have recently inherited a Grails 2 application that I'm upgrading to Grails 5. It has a LOT of GMongo-specific code. I can still use GMongo in Grails 5 as we are still targeting a legacy MongoDB 3.4.2 database.
However, I am also being asked to add PowerBI support to MongoDB, which requires an upgrade to at least MongoDB v4.0 (or MongoDB 5.0 if I want to use the Grails MongoDB plugin).
Question 1:
GMongo is a wrapper around the java-mongo-driver
. What is the latest version of MongoDB GMongo 1.5 can support? Is it MongoDB v.3.x.x at most?.
Question 2:
This one is probably a long shot...but is there any replacement for GMongo in Grails 5/GORMfor MongoDB 5.0? The code base I have inherited has many thousands of lines of GMongo-specific code and if I want to upgrade MongoDB to support PowerBI, it means rewriting all of these using a recent Java Mongo driver.
It's a lot of work that I suspect I will have do do regardless, but I figure there's no harm in asking the GMongo author himself!
I'm also a little curious why this project was seemingly abandoned 8 years ago. Is it just not worth the time and effort any more given that Groovy and Grails aren't so popular?
Thanks in advance for any answers given. It's much appreciated.
when I use gmongo in soapUI like this:
def cursor = db.contactInfo.find(broadcastId: "${broadcastId}")
result.count= cursor.count()
I always get count=10, but if I query from mongo shell directly, the result is 1, is it a bug or I am using it in wrong way?
Thanks!
Hi,
I'm experiencing memory leaks since I started to use gmongo.
I read few mailing list post that you already identified and fixed a memory leak in v.0.7-0.8
The Eclipse Memory Analyzer identifies the following leak:
2,267 instances of "groovy.lang.ExpandoMetaClass",
loaded by "org.apache.catalina.loader.WebappClassLoader @ 0xf5538ae8"
occupy 61,576,696 (46.90%) bytes.
These instances are referenced from one instance of "java.lang.Object[]", loaded by "<system class loader>"
Keywords
java.lang.Object[]
groovy.lang.ExpandoMetaClass
org.apache.catalina.loader.WebappClassLoader @ 0xf5538ae8
Looking at the heapdump those instances of ExpandoMetaClass is then holding MetaMethodIndex instances (several hundreds of thousands) for the same class
"DBApiLayer$MyCollection" and "DBCursor" with one set of methods of every method in the driver api.
My suspect is that the groovy delegates are continuously generated (multiple times) and retained by the class loader, so that you have multiple times the same delegates generated over and over for the same target Class.
Have you came across this problem?
regards
Bruno
Hello,
I'm testing gmongo and can't find an example of how to add an item to an array, for example my document is like: [name: 'XXX', cars:[]]
After I create that I need to add a car [brand:'xxx', model:'xxx'] to the person's cars array.
Can you give me any pointers of how to do that "the groovy way".
I've tried things like person.cars << [....] but doesn't seem to save anything.
Thanks a lot!
Hi,
I have posted a question on Stackoverflow on how to use Finalize.
http://stackoverflow.com/questions/24723350/gmongo-mapreducer-with-finalizer
As explained in the post, was not able to use the mapreducecommand. Could I get help here?
Thanks
def x = [monkey: 23]
db.collection.save(x)
assertNotNull x._id
This will fail.
The reason, I assume, is because under the hood, you create a BasicDBObject under the hood from the Map,
then call save() using that BasicDBObject.
The problem is that the _id is lost.
I fixed it in my code by explicitly creating a BasicDBObject.
def x = new BasicDBObject(monkey: 23)
db.collection.save(x)
assertNotNull x._id
And that's fine.
It's just wierd, because it is different from the mongosh behavior for save() on a JSON object.
The java mongo driver has some features we need in 2.13 any chance this plugin could be modded to support?
It would be nice if GMongoClient had constructors equivalent to the following:
// Creates a Mongo based on a list of replica set members or a list of mongos.
MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList)
// Creates a Mongo based on a list of replica set members or a list of mongos.
MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options)
Is there any chance you could maintain a 1.7 build in maven for gmongo updates? I'm using grails 1.3.7, so I'm locked into 1.7.10. I'm investigating what it would take to move to Grails 2 right now, but it would be much simpler if there was a 1.7 build.
By moving to 1.8, all 1.3.7 Grails users are locked out. :/
In the java mongo driver MongoClient
actually extends Mongo
. So i think there can be some slight simplification here and addition of this chain of inheritence so it would be more consistent to use. We are running into a case where that was assumed and its failing as a result. We are working on a PR with a fix. just wanted to give you a heads up. Thanks on releasing 1.4 btw.
I use gmongo within a grails app and I tried bumping to v0.9.3 and ran into this error when trying to startup. I didn't see anything upgrade notes in the commit log. Is there something I need to update?
Thanks for a great library btw
2012-01-23 16:13:46,817 [Thread-8] ERROR context.GrailsContextLoader - Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: static com.gmongo.internal.MirrorObjectMutation.copyGeneratedId() is applicable for argument types: (com.brightcove.reporting.Domain, com.brightcove.reporting.Domain) values: [com.brightcove.reporting.Domain : null, ...]
Possible solutions: copyGeneratedId([Ljava.lang.Object;, [Ljava.lang.Object;), copyGeneratedId(java.util.List, java.util.List), copyGeneratedId(java.util.Map, java.util.Map)
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static com.gmongo.internal.MirrorObjectMutation.copyGeneratedId() is applicable for argument types: (com.brightcove.reporting.Domain, com.brightcove.reporting.Domain) values: [com.brightcove.reporting.Domain : null, ...]
Possible solutions: copyGeneratedId([Ljava.lang.Object;, [Ljava.lang.Object;), copyGeneratedId(java.util.List, java.util.List), copyGeneratedId(java.util.Map, java.util.Map)
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:290)
at grails.util.Environment.executeForEnvironment(Environment.java:283)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:259)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: groovy.lang.MissingMethodException: No signature of method: static com.gmongo.internal.MirrorObjectMutation.copyGeneratedId() is applicable for argument types: (com.brightcove.reporting.Domain, com.brightcove.reporting.Domain) values: [com.brightcove.reporting.Domain : null, ...]
Possible solutions: copyGeneratedId([Ljava.lang.Object;, [Ljava.lang.Object;), copyGeneratedId(java.util.List, java.util.List), copyGeneratedId(java.util.Map, java.util.Map)
at com.gmongo.internal.DBCollectionPatcher$__clinit__closure3.doCall(DBCollectionPatcher.groovy:41)
at com.gmongo.internal.Patcher._invoke(Patcher.groovy:50)
at com.gmongo.internal.Patcher$__patchInternal_closure1.doCall(Patcher.groovy:38)
at com.brightcove.reporting.DomainService.save(DomainService.groovy:48)
at com.brightcove.reporting.DomainService.demoData(DomainService.groovy:136)
at BootStrap$_closure1.doCall(BootStrap.groovy:14)
... 8 more
First off, great project!
Writing to check when the latest GMongo bits will make it to Maven.
Here's why:
I'm using gmongo within a grails project via the MongoDB plugin. I used your "clicks" group() example from the readme and changed it to an aggregate(), which gave me an error that a linkedHashMap couldn't be used. I checked the latest gmongo source and see that aggregate() is added to the patched methods list, which presumably would let me do this successfully:
db.clicks.aggregate(
[$group : [ _id : '$day' , clickCount : [$sum : '$total']] ]
)
Without the patch, I'm stuck with this:
def clicks = db.clicks.aggregate(
new BasicDBObject( [$group : [ _id : '$day' , clickCount : [$sum : '$total']] ] )
)
At this point I'm assuming that if I could point my project to the latest GMongo bits this would work fine.
Any estimate on how long it'll be before the latest code makes its way to maven?
Thanks!
It would be great if you could integrate Bulk operations from MongoDB 2.6 such as initializeOrderedBulkOperation(). The Java Drivers are supporting them already: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#bulk-operations
Not sure where else to ask this question.
Is it possible to connect gmongo to the mongo database via a jndi lookup?
In the Java driver you can specify using a cursor as an AggregationOption, but I'm not sure how to do this in gmongo. By default it doesn't use a cursor, and aggregation results are therefore limited to 16MB.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.