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/
Related
I need to call custom oracle function from spring boot application without using native query.
below is my oracle function which is taking Date as input
create or replace FUNCTION todate(src_dt IN date)
RETURN date
is
BEGIN
RETURN(to_date(src_dt));
END;
i was searching solution on internet but so far did't find. people are saying some custom dialect need to create but not found any perfect step by step link.
below is my java code :-
Query query1 = entityManager.createQuery("select todate(ActSubT.createdDt) from ActSubT ActSubT");
List<Object> result=query1.getResultList();
this code should run , as of now its giving error as todate is oracle function and i haven't configured anything in application.yml file.
Please help
I'm using hibernate-search-elasticsearch 5.8.2.Final and I can't figure out how to get script fields:
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-script-fields.html
Is there any way to accomplish this functionality?
This is not possible in Hibernate Search 5.8.
In Hibernate Search 5.10 you could get direct access to the REST client, send a REST request to Elasticsearch and get the result as a JSON string that you would have to parse yourself, but it is very low-level and you would not benefit from the Hibernate Search search APIs at all (no query DSL, no managed entity loading, no direct translation entity type => index name, ...).
If you want better support for this feature, don't hesitate to open a ticket on our JIRA, describing in details what you are trying to achieve and how you would have expected to be able to do that. We are currently working on Search 6.0 which brings a lot of improvements, in particular when it comes to using native features of Elasticsearch, so it just might be something we could slip into our backlog.
EDIT: I forgot to mention that, while you cannot use server-side scripts, you can still get the full source from your documents, and do some parsing in your application to achieve a similar result. This will work even in Search 5.8:
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(entityManager);
FullTextQuery query = fullTextEm.createFullTextQuery(
qb.keyword()
.onField( "tags" )
.matching( "round-based" )
.createQuery(),
VideoGame.class
)
.setProjection( ElasticsearchProjectionConstants.SCORE, ElasticsearchProjectionConstants.SOURCE );
Object[] projections = (Object[]) query.getSingleResult();
for (Object projection : projections) {
float score = (float) projection[0];
String source = (String) projection[1];
}
See this section of the documentation.
I'm new user of mongoDB, I have create an application with spring boot/mySql and I want replace database sql to mongoDB.
I did change all things necessary for example, annotations, relations etc ... but I have a problem with request I don't know the similar syntax of #Query in mongoDb.
this my request with sql:
// this calculate the sum of all distance in walk's document
#Query("select sum(distance) from Walk")
public Double sumDistance();
I did read mongoDB aggregate and I know how to use it in console windows with command
$ mongo
$ db.walk.aggregate([{$group:{"_id":"$dateWalk",Count:{$sum:"$distance"}}}])
but, I want use $sum in my program java not in console.
So please, I need your help about the syntax similar to this with mongoDB
// this calculate the sum of all distance in walk's document
#Query("select sum(distance) from Walk")
public Double sumDistance();
thanks in advance :)
You would need to create a custom Repository that would implement the aggregation.
Examples:
https://www.javacodegeeks.com/2016/04/data-aggregation-spring-data-mongodb-spring-boot.html
https://github.com/spring-projects/spring-data-examples/blob/master/mongodb/aggregation/src/main/java/example/springdata/mongodb/aggregation/OrderRepositoryImpl.java
I have mongodb aggregation query and it works perfectly in shell.
How can i rewrite this query to use with morphia ?
org.mongodb.morphia.aggregation.Group.addToSet(String field) accepts only one field name but i need to add object to the set.
Query:
......aggregate([
{$group:
{"_id":"$subjectHash",
"authors":{$addToSet:"$fromAddress.address"},
---->> "messageDataSet":{$addToSet:{"sentDate":"$sentDate","messageId":"$_id"}},
"messageCount":{$sum:1}}},
{$sort:{....}},
{$limit:10},
{$skip:0}
])
Java code:
AggregationPipeline aggregationPipeline = myDatastore.createAggregation(Message.class)
.group("subjectHash",
grouping("authors", addToSet("fromAddress.address")),
--------??????------>> grouping("messageDataSet", ???????),
grouping("messageCount", new Accumulator("$sum", 1))
).sort(...)).limit(...).skip(...);
That's currently not supported but if you'll file an issue I'd be happy to include that in an upcoming release.
Thanks for your answer, I can guess that according to source code. :(
I don't want to use spring-data or java-driver directly (for this project) so I changed my document representation.
Added messageDataSet object which contains sentDate and messageId (and some other nested objects) (these values become duplicated in a document which is a bad design).
Aggregation becomes : "messageDataSet":{$addToSet:"$messageDataSet"},
and Java code is: grouping("messageDataSet", addToSet("messageDataSet")),
This works with moprhia. Thanks.
I am new to Mongo DB can we insert the data in java and retrieve the data from Mongo DB using java script?
Please suggest.
Take a look into mongodb quick start
Take a look into mongodb java tutorial. Here you can find how to connect/query/update mongodb.
To retrieve data from mongodb via javascript create some service layer (it can be simple rest api) that will return data in json format for example.
Javascript way of printing output of MongoDB query to a file
1] create a javascript file
test.js
cursor = db.printShardingStatus();
while(cursor.hasNext()){
printjson(cursor.next());
}
2] run
mongo admin --quiet test.js > output.txt