I was answered about constructing a MongoDb query
How to code collection find with regex and complex criteria using Java MongoDB driver?
Still the question about a count was unanswered:
So for this query how to write an analogous Java code returning count:
db.cache.find({objectKey: { $regex: 'Bos*'}, cacheVersionString:'08/03/15_11:05:09'}).count()
With MongoTemplate, it would be something like this:
Query query = new Query();
query.addCriteria(Criteria.where("objectKey").regex("Bos*"));
query.addCriteria(Criteria.where("cacheVersionString").is("08/03/15_11:05:09"));
long count = mongoTemplate.count(query, MyClass.class);
Related
Using the Elasticsearch High Level REST Client for Java v7.3
I have a few fields in the schema that look like this:
{
"document_type" : ["Utility", "Credit"]
}
Basically one field could have an array of strings as the value. I not only need to query for a specific document_type, but also a general string query.
I've tried the following code:
QueryBuilder query = QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(terms))
.filter(QueryBuilders.termQuery("document_type", "Utility"));
...which does not return any results. If I remove the ".filter()" part the query returns fine, but the filter appears to prevent any results from coming back. I'm suspecting it's because document_type is a multi-valued array - maybe I'm wrong though. How would I build a query query all documents for specific terms, but also filter by document_type?
I think, the reason is the wrong query. Consider using the terms query instead of term query. There is also a eqivalent in the java api.
Here is a good overview of the query qsl queries and their eqivalent in the high level rest client: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html
I have a collection in mongodb which has many documents. Using Studio 3T I can see the document looks like below
{
"DialectType" : "ORACLE",
"DomainName" : "NewDomain"
}
There are many of this type with different values but with same keys. I am using below code to query the documents-
Query query = Query.query(Criteria.where("DialectType").is("ORACLE"));
mongoOperations.find(query, DialectTypeCollection.class, "my_collection_name");
The above query does not return records. I am not sure what is the issue. Any help is appreciated.
I'm currecntly trying to test out some new score functions on my Elasticsearch query but I'm not yielding the results I am expecting.
I found this on their site about explaining queries here
I can run as a curl command but does anyone know how to translate this to use the Java api?
If you're using the ES5 Java API, you can get the explanation like this:
QueryBuilder query = matchAllQuery(); // your query
ExplainRequest request = new ExplainRequest("index", "type", "id").query(query);
ExplainResponse explainResponse = client.explain(request).actionGet();
Explanation explanation = explainResponse.getExplanation();
Where client is your org.elasticsearch.client.Client instance.
I have a collection in mongo which contains 6 documents .When I run query directly in mongo it is qorking fine.But when I run the same query in spring , I am not getting the result
I have the following query
Mongo DB: db.getCollection('table_name').find({"column_1" : "value_1" })
Spring :
Query q = new BasicQuery("{ column_1: 'value_1'}");
this.mongoOps.find(q, TableName.class, "table_name");
I tried with different mongo versions ans with different spring versions but not working.What might be the issue here.
NOTE:Query is working with JDBC as well
Thanks in advance...
If you're considerating use Query class, try add Criteria
Query query = new Query();
query.addCriteria(Criteria.where("field_1").in("value_1"));
Edit
If you want to use BasicQuery, try:
BasicQuery query1 = new BasicQuery("{ 'field': 'value_1' }");
User userTest1 = mongoOperation.findOne(query1, YourClass.class);
Remember in Mongo we don't call column to fields, because it doesn't has column :)
I cannot for the life of me find out how to get a count for a find query using the java driver in mongo db. Can someone please put me out of my misery?
I have the following:
MongoCursor<Document> findRes = collection.find().iterator();
But there is no count method that I can find anywhere.
public Long getTotalCount(String collectionName, Document filterDocument) {
MongoCollection collection = database.getCollection(collectionName);
return filterDocument != null ? collection.count(filterDocument) : collection.count();
}
Where filterDocument is org.bson.Document with filter criterias or null if you want to get total count
You may also use more powerful Filters class. Example: collection.count(Filters.and(Filters.eq("field","value"),second condition and so on));
So, in order to be able to take both Document and Filters as param you may change signature to public Long getTotalCount(String collectionName, Bson filterDocument) {
long rows = db.getCollection(myCollection).count(new Document("_id", 10)) ;
this is in Java, myCollection is collection name.
MongoDB has inbuilt method count() that can be called on cursor to find the number of documents returned.
I tried following piece of code in mongodb, that worked well, can be easily applied in java or any other language too:
var findres = db.c.find()
findres.count() gave output 29353
cursor.count() is what you're looking for I believe. Your find query returns a Cursor so you can just call count() on that.