I want to know how to write a Morphia mongodb query with 'or' operator
I wrote mongodb query like this and this work fine
db.Inv.find({$or:[{sug_id:2},{grp_id:2}]})
But i got confused when i try to write this in morphia, following query is wrong but how can write something similar to this
List<Inv> invs = ds.find(Inv.class).field("grp_id").hasAnyOf(grpId).or(field("sug_id")).hasAnyOf(grpId).asList();
Thanks
not sure why hasAnyOf() is in there but try this:
Query<Inv> query = ds.find(Inv.class);
query.or(
query.criteria("grp_id").equal(2),
query.criteria("sug_id").equal(2));
List<Inv> invs = query.asList();
Related
I am new in Mongodb. can anybody tell me any online tool where i can easily convert my sql/ oracle query in mongodb for my java code.
ex:
Simple oracle query:
select * form student where class = "XII" and name="John";
MongoDb Query for java:
db.getCollection("Student", Student.class).find(and(eq("class" , "XII"), eq("name", "John"))).into(new ArrayList<Employee>());
so i want a query builder for java where i can pass the sql/oracle query and i will get Java code for mongodb as Output.
Please suggest some useful .
I don't think if there is any converter available but I will suggest you to learn it by this best free quick course
https://university.mongodb.com/courses/MongoDB/M101J/
I am using Java Spring to work with Mongodb. I need to find documents which the word 'manager' is existed in description field. I tried following two method
Method 1
Query query = new Query();
query.addCriteria(Criteria.where("discription").regex("/\bmanager\b/"));
Method 2
Query query = new Query();
Pattern p = Pattern.compile("/\bmanager\b/");
query.addCriteria(Criteria.where("discription").regex(p));
But none of these were worked. I tried it with mongodb console like this
db.test.find({discription: {$regex: /\bmanager\b/}})
It worked as I expected. What's wrong with my Java code.
You don't have to add the slashes in the regex expression, as the regex method takes care of it. So
Query query = new Query();
query.addCriteria(Criteria.where("description").regex("\bmanager\b"));
should work.
It looks like you can just pass your regex string straight through without using Pattern.compile(). Have you tried that?
I need help with converting an SQL-Query to something I can use inside play!
The original Query looks like this:
SELECT * FROM `triplestore`
WHERE `owning_admin_id` IS NOT NULL
AND `is_public` IS true;
My goal is to get something like
Triplestore.find.where()
.isNotNull("owningAdmin")
.is("is_public", true)
.findList();
Is this even possible or do I have to use JPQL? What would that Query look like?
Thanks in advance,
Hagen
You can use the and() method:
Triplestore.find.where().and(Expr.isNotNull("owningAdmin"),Expr.eq("is_public", true)).findList();
Don't forget to add the com.avaje.ebean.Expr import.
In Hibernate Criteria:
List<Triplestore> triplestores =
(List<Triplestore>) session.createCriteria(Triplestore.class)
.add( Restrictions.isNotNull("owning_admin_id") )
.add( Restrictions.eq("is_public", Boolean.TRUE) ).list();
Regards,
You said "something I can use inside play!", so I'm assuming any third party library is OK. Here's a solution using jOOQ (disclaimer: I work for the vendor).
I'm assuming you're using jOOQ's code generator here:
Result<TriplestoreRecord> result =
ctx.selectFrom(TRIPLESTORE)
.where(TRIPLESTORE.OWNING_ADMIN_ID.isNotNull())
.and(TRIPLESTORE.IS_PUBLIC)
.fetch();
Currently I am using java to connect to MONGODB,
I want to write this sql query in mongodb using java driver:
select * from tableA where name like("%ab%")
is their any solution to perform the same task through java,
the query in mongodb is very simple i know, the query is
db.collection.find({name:/ab/})
but how to perform same task in java
Current I am using pattern matching to perform the task and code is
DBObject A = QueryBuilder.start("name").is(Pattern.compile("ab",
Pattern.CASE_INSENSITIVE)).get();
but it makes query very slow I think , does a solution exist that does not use pattern matching?
Can use Regular Expressions. Take a look at the following:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
Make sure you understand the potential performance impacts!
DBObject A = QueryBuilder.start("name").is(Pattern.compile("ab",
Pattern.CASE_INSENSITIVE)).get();
I think this is one of the possible solution, you need to create index to achieve those.
Why do you fear the regular expressions? Once the expression is compiled they are very fast, and if the expression is "ab" the result is similar to a function that search a substring in a string.
However to do what you need you have 2 possibilities:
The first one, using regular expression, as you mention in your question. And I believe this is the best solution.
The second one, using the $where queries.
With $where queries you can specify expression like these
db.foo.find({"$where" : "this.x + this.y == 10"})
db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"})
and so you can use the JavaScript .indexOf() on string fields.
Code snippet using the $regex clause (as mentioned by mikeycgto)
String searchString = "ab";
DBCollection coll = db.getCollection("yourCollection");
query.put("name",
new BasicDBObject("$regex", String.format(".*((?i)%s).*", searchString)) );
DBCursor cur = coll.find(query);
while (cur.hasNext()) {
DBObject dbObj = cur.next();
// your code to read the DBObject ..
}
As long as you are not opening and closing the connection per method call, the query should be fast.
i'm trying to do a $ne query in mongodb while using a regex, but it doesn't seem to work. the $ne (not equal) operator works fine when i don't use a regex though.
BasicDBObject q = new BasicDBObject()
q.put(field, ["\$ne": value])
the above works fine, the result set doesn't contain any documents that has that value for that field.
but i need it to be case insensitive. so i did this
q.put(field, ["\$ne": Pattern.compile(value, Pattern.CASE_INSENSITIVE)])
but this doesn't work..
so i thought, let me go to the command line and see if i can do it manually. so i did this:
db.Order.find({"bill.recipient.name": {$ne: /diep/i}},{"bill.recipient.name":1})
and it still doesn't work!
any ideas?
Try using $not instead of $ne.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%24not
You can construct your query this way also
BasicDBObject query = new BasicDBObject();
query.append(fieldname,new BasicDBObject("$ne", value));