In mongodb java driver how to sort by using two fields - mongodb-java

using mongo java driver I am able to sort like so:
.sort(new BasicDBObject("firstColumn",1));
How do I sort by two fields?

You can try something like below.
.sort(new BasicDBObject("firstColumn", 1).append("SecondColumn", 1));

Related

Using ElaasticSearch script in the Grafana

I’m going to use this ElasticSearch query in the Grafana:
But the result in Grafana is something like this:
How can I remove the field part in the Grfafana?
I manipulated the native ElasticSearch data source plugin. Now if the field does not have value and the script has, It will only use the script in an inline tag.
The result is something like this:
“aggs”:{“3”:{“sum”:{“script”:{“inline”:“Double.parseDouble(doc[‘load-value’].value)”}}}}

What's the different between containing and like in JPA?

If I wanna use fuzzy select , It seems wrong when I used like ,I have to use containing . So What's the different between containing and like in JPA?
Both are very similar, according Spring documentation on Container parameters are bound wrapped in % and on Like not. So if you want same result, try to add % to parameters on query with Like
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details

How do I combine Facet and FilterQueries using Spring data Solr?

Is it possible to combine a facet and field query in spring data solr? Something that would build a query like this:
> http://localhost:8983/solr/myCore/select?q=lastName%3AHarris*&fq=filterQueryField%3Ared&wt=json&indent=true&facet=true&facet.field=state
In other words, how do I add FilterParameters to a SimpleFacetQuery?
Any/all replies welcome, thanks in advance,
-- Griff
I assume you're using Spring Data Solr, from your reference to SimpleFacetQuery. Based on your sample query, the code would look something like this:
// creates query with facet
SimpleFacetQuery query = new SimpleFacetQuery(
new Criteria("lastName").startsWith("Harris"))
.setFacetOptions(new FacetOptions()
.addFacetOnField("state"));
// filter parameters on query
query.addFilterQuery(new SimpleFilterQuery(
Criteria.where("filterQueryField").is("red")));
// using query with solrTemplate
solrTemplate.queryForFacetPage(query, YourDocumentClass.class)

How to get last N documents with mongoid?

I have found some information to accomplish this in mongoDB, but I need it with mongoid. So I can do something like:
User.last(7000).each do ....
I'm using:
MongoDB shell version: 2.4.3
Mongoid 2.6.0
Thanks!
Now I found a solution from mongoid origin:
User.all.desc('_id').limit(7000)
It sorts the users in descending order according to the id.
Aggregations can directly be chained to a Model in rails irrespective of the ORM used. We don't need to use all. You could just do the following
User.limit(3000).desc('_id')
This will only return the Mongoid criteria. To see the data you can chain .all to the criteria. You can directly do operations on the criteria also, without having to chain .all or to_a.

mongo subtract two fields

I have a mongo collection which has documents with two fields "fieldA" and "fieldB", which are timestamps and I need to get all the documents which are "(fieldB-fieldA)/6000 > 2"... So I was looking for some function in order to do this...
I saw in some posts the function "$subtract" but that seems is in mongo 2.1.x and currently I'm using 2.0.x (it's the stable), any idea how to do this with 2.0.x? or Do I need to create a new field? or Can I only do it in the application side?
btw, I'm using the ruby mongo driver...
You can use the $where operator to specify query expressions as Javascript. In the shell:
db.myCollection.find( { $where: "(this.fieldB - this.fieldA)/6000 > 2" } );
You should note, however, that Javascript execution can be very slow. If this is a one time query, $where may be an ok solution. Otherwise, storing the result of the equation in a separate field is the best way to ensure that queries are timely.
Additional information on the $where operator:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

Resources