Reputation: 1232
I have a following query :
{ query: { "$where" : { this.userName == 'Jack123' } } }
Note:- Please don't suggest query.where()
Upvotes: 1
Views: 1370
Reputation: 11
In Morphia v1.3.2 to access the createQuery method that recieves a DBObject argument you have to cast datastore to AdvancedDatastore.
Like:
Query<MyClass> myQuery = ((AdvancedDatastore) myDatastore).createQuery(MyClass.class, myDbObject);
Upvotes: 0
Reputation: 458
From documentation:
You can use Morphia to map queries you might have already written using the raw Java API against your objects, or to access features which are not yet present in Morphia.
For example:
DBObject query = BasicDBObjectBuilder.start()
.add("albums",
new BasicDBObject("$elemMatch",
new BasicDBObject("$and", new BasicDBObject[] {
new BasicDBObject("albumId", albumDto.getAlbumId()),
new BasicDBObject("album",
new BasicDBObject("$exists", false))})))
.get();
Artist result = datastore.createQuery(Artist.class, query).get();
Hope it'll help you.
Upd.:
Alternatively you can try this:
DBCursor dbCursor = datastore.getCollection(MyClass.class).find(query);
while (dbCursor.hasNext()) {
DBObject obj = dbCursor.next();
MyClass class = morphia.fromDBObject(datastore, MyClass.class, obj);
// do stuff
}
Upvotes: 0
Reputation: 6243
Another option if you already have the query in raw string format is to use BasicDBObject.parse()
to parse your query in to a DBObject
then build a Query
using that DBObject
. It should be noted that if this string comes from user data you should take care to make sure you're not executing arbitrary queries that could fetch data not authorized for the user submitting that query.
Upvotes: 2
Reputation: 6243
@Marcus-rool's first answer is the correct approach. You can use fromDBObject()
though you shouldn't. It's exposed publicly largely due to history and limitations in Java's access controls. But it should be noted that that method is deprecated in 1.5.0 (not yet released) and will be removed/replaced in 2.0.0. It's really an internal method that got exposed for $reasons
but was never intended to be used externally.
Upvotes: -1