This question already has answers here:
MongoDB GUI client (cross-platform or Linux) [closed]
(5 answers)
Closed 3 years ago.
I need to perform the following MongoDB query in Java:
db.ventas.aggregate([
{
$sort: {"codArt._id": -1}
},
{
$group:{
_id: "$codArt._id",
denominacion: {"$first": "$codArt.denominacion"},
unidades: {"$sum": "$unidades"},
importe: {"$first": {"$multiply": [{"$sum": "$unidades"}, "$codArt.pvp"]}},
stock: {"$first": {"$subtract": ["$codArt.stock", "$unidades"]}}
}
}
])
Is there any library, which can do it?
Unfortunately, I can't install MongoDB Compass.
You could use something like Hibernate, but no automatic translation is provided... you'll need to build your model. It will be useful though if you do not want to build the inner query or need some portability.
http://hibernate.org/ogm/
There is a great tool to learn MongoDB Java Syntax and to work with MongoDB in general: Studio 3T, there you can generate a MongoDB query for the Java driver. Trial 30 days.
P.S. I'm not sure if this software requires admin rights to be installed.
Related
This question already has answers here:
LocalDateTime and SQL Server JDBC 4.2 driver
(4 answers)
Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
(1 answer)
Closed last year.
I was looking for a solution to my problem. I was coding my JdbcImpl file and I got an error when I was coding this :
rs.getDate create an error in a screen
Enchere enchereResultSet = new Enchere(
//Changer le getDate to LocalDateTime
rs.getDate("date_encheres"),
rs.getInt("montant_enchere"),
rs.getInt("no_article"),
rs.getInt("no_utilisateur"));
I found the fix! It seems simple, just go for this instead:
Enchere enchereResultSet = new Enchere(
//Changer le getDate to LocalDateTime
rs.getTimestamp("date_enchere").toLocalDateTime(),
rs.getInt("montant_enchere"),
rs.getInt("no_article"),
rs.getInt("no_utilisateur"));
The solution in a screen
Do I have the right to share the fix if I found it while writing a post here? I share it because it will maybe help beginner like me in the future.
This question already has answers here:
Use $strLenCP with Spring Data MongoDB
(3 answers)
Closed 2 years ago.
I have a mongoDB aggregate query using which i was able to get the length of the field which has max no of characters in the collection. I need help to convert that aggregate query to its equivalent in java.
Please find the aggregate query below:
db.getCollection('staff').aggregate([
{"$match": {"department": "technology"}},
{"$project": {"maxCharLength": {"$strLenCP": "$firstName"}}},
{"$sort": {"maxCharLength": -1}},{"$limit": 1}
])
I need to convert the above query to its equivalent in java. Please find the java code which im trying below: Im stuck with on how to use $strLenCP with project in java code below:
Aggregation agg = newAggregation(
match(Criteria.where("department").in("technology")),
project(""), //how to use $strLenCP here
sort(Sort.Direction.DESC, "maxCharLength"),
limit(1));
mongoTemplate.aggregate(agg, "staff", Staff.class);
Aggregation.project("firstName").andExpression("strLenCP(firstName)").as("length")
I am using cassandra-2.0.10 and hector api.
I have tried :
public static void createCounterColumnFamily(Keyspace keyspace, String ccfName) {
Mutator<String> mutator = HFactory.createMutator(keyspace,StringSerializer.get());
mutator.addCounter("salary", ccfName, HFactory.createCounterColumn("salary", 10L));
mutator.execute();}
But, I'm getting this exception :
Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:unconfigured columnfamily counter_column_family_1)
at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:45)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:265)
at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecuteOperation(ExecutingKeyspace.java:113)
at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243)
at com.cassandra.practice.CounterColumnFamily.createCounterColumnFamily(CounterColumnFamily.java:18)
at com.cassandra.practice.Bootstrapper.main(Bootstrapper.java:33)
Caused by: InvalidRequestException(why:unconfigured columnfamily counter_column_family_1)
at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20833)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:246)
at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:243)
at me.prettyprint.cassandra.service.Operation.executeAndSetResult(Operation.java:104)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:258)
... 4 more
Am I missing something ?
You need to explicitly create both the column family and the keyspace before you can insert data.
That said the Hector API was deprecated more than five years ago and the Cassandra version you are using is just as far behind. More significantly, the Thrift API has been deprecated completely for years and has already been removed from trunk in the upcoming 4.0 release.
Please switch to 3.11.4 of Cassandra available here:
http://cassandra.apache.org/download/
And to using CQL and the java driver for such available here:
https://github.com/datastax/java-driver/tree/3.x/manual
Out of curiosity, how did you come across the versions you are currently using?
I'm experimenting with Java and Couchbase 6.0 Community edition using Java 2.7 SDK.
I'm trying to execute a simple update query from my java application the Couchbase Java 2.7 SDK:
String query ="UPDATE admin SET FIELDNAME='TEST'"
N1qlParams params = N1qlParams.build().adhoc(false);
N1qlQuery nquery = N1qlQuery.simple(query, params);
N1qlQueryResult nqr= this.rbucket.query(nquery);
And I am getting the following exception (the most meaningful part):
com.couchbase.client.core.CouchbaseException: N1qlQuery Error - {"msg":"syntax error - at UPDATE","code":3000}
The actual exception starts like this:
Exception in thread "main" com.couchbase.client.core.CouchbaseException: Error while preparing plan
Of course - this query works fine through the Couchbase web UI and I can update without problem.
Just for info: I tried escaping the single quotes, even tried setting the column to be equal to itself - same error.
Select queries are executed in a similar manner without any problem.
"admin' is not a good name for a bucket, as it is usually related to some reserved keywords, if you still want to use this name, you have to use backticks around it:
update `admin` set FIELDNAME = 'TEST'
It also might ask you to create a primary index if you don't have one yet.
I am new to ElasticSearch. I want to convert below query:
GET /snomed_v1/_analyze?analyzer=english
{ 12 O'clock, superior margin }
to the Java api query.
Thanks.
Connect to your ES cluster as per the basics here: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html
Your query will ask the admin interface for an AnalyzeRequest(index, query) with the english analyzer:
ESClient.admin().indices().analyze(new AnalyzeRequest("snomed_v1", "12 O'clock, superior margin").analyzer("english"));