Context
I need to rewrite some code previoulsy with Jongo but using Springframework MongoDb. Previous code was:
eventsCollection
.aggregate("{$match:" + query + "}")
.and("{$group: {_id: '$domain', domain: {$first: '$domain'}, codes: {$push: '$code'}}}")
.and("{$project : { _id: 0, domain: 1 , codes: 1 } }")
.as(DomainCodes.class);
where eventsCollection is Jongo MongoCollection, and query is a String containing with criteria.
Problem
New code must probably look like :
Aggregation myAggregation = Aggregation.newAggregation(
Aggregation.match(/* something here */),
Aggregation.group("domain").first("domain").as("domain").push("code").as("codes")
);
mongoTemplate.aggregate(myAggregation, "collectionName", DomainCodes.class);
but I don't find a way to create match criteria using String (similare as BasicQuery that can take a query as String for argument)
Question
In order to change as little code as possible, is there anyway to use query String as in Jongo ?
Thank you,
Related
How I can update a string field in a mongo document, concatenating another string value, using java and spring-data mongo? Ex:
{
“languages”: “python,java,c”
}
Concat “kotlin”:
{
“languages”: “python,java,c,kotlin”
}
Thanks so much.
Starting in MongoDB v4.2, the db.collection.update() method can accept an aggregation pipeline to modify a field using the values of the other fields in the Document.
Update with Aggregation Pipeline
Try this one:
UpdateResult result = mongoTemplate.updateMulti(Query.query(new Criteria()),
AggregationUpdate.update()
.set(SetOperation.set("languages")
.toValue(StringOperators.Concat.valueOf("languages").concat(",").concat("kotlin"))),
"collection"); //mongoTemplate.getCollectionName(Entity.class)
System.out.println(result);
//AcknowledgedUpdateResult{matchedCount=1, modifiedCount=1, upsertedId=null}
In MongoDB shell, it looks like this:
db.collection.updateMany({},
[
{ "$set" : { "languages" : { "$concat" : ["$languages", ",", "kotlin"]}}}
]
)
in my case i use a dataset(dataframe) in JavaSparkSQL.
This dataset result from an JSON file. The json file is formed from key-value.When i lunch a query for see the value i write for examle:
SELECT key1.name from table
example JSON file
{
"key1":
{ "name": ".....",....}
"key2":
{ "name":"....",....}
}
my question is, when i want acceding at all key,I believe I should use a REGEX like
select key*.name from table
but i don't know the regex!
please help
I am afraid no such syntax is available in (spark) SQL.
You may want to construct your query programmatically though.
Something like :
String sql = Stream.of(ds.schema().fieldNames()).filter(name -> name.startsWith("key")).collect(Collectors.joining(", ", "select ", " from table"));
System.out.println(sql);
or even
Dataset<Row> result = spark.table("table").select(Stream.of(ds.schema().fieldNames()).filter(name -> name.startsWith("key")).map(name -> ds.col(name))
.toArray(Column[]::new));
result.show();
HTH!
I use this search method in my Spring data/MongoDB application:
#Query("{$text : { $search : ?0 } }")
List<DocumentFile> findBySearchString(final String searchString);
My question would be what I have to give in for searchString that I get DocumentFiles where two terms exists (e.g. term1 AND term2 or term1 + term2).
Is there something like AND with Spring Data and MongoDB?
Yes, you can achieve using mongo repository
List<DocumentFile> findByString1AndString2(String String1,String String2);
Ok, based on your last round of comments-can we do like this
Using mongo template
Criteria regex = Criteria.where("name").regex("*.xxxx.*", "i");
mongoOperations.find(new Query().addCriteria(regex), DocumentFile.class);
Using mongo repository
List<DocumentFile> documents= repository .findByQuery(".*ab.*");
interface repository extends MongoRepository<DocumentFile, String> {
#Query("{'name': {$regex: ?0 }})")
List<DocumentFile> findByQuery(String name);
}
Hope it will useful.
I'm new in mongodb. I have following data as a JSON format in mongodb. I need to search the bookLabel or the shortLabel for the book and it should show me all the information about the book. For example: if I query for 'Cosmos' it'll show all the description about the book, like: bookLabel, writer, yearPublish, url. How can I do that in java? Need query, please help.
"Class":"Science",
"Description":[
{
"bookLabel":"Cosmos (Mass Market Paperback)",
"shortLabel":"Cosmos",
"writer":"Carl Sagan",
"yearPublish":[
"2002"
],
"url":"https://www.goodreads.com/book/show/55030.Cosmos"
},
{
"bookLabel":"The Immortal Life of Henrietta Lacks",
"shortLabel":"Immortal Life",
"writer":"Rebecca Skloot",
"yearPublish":[
"2010, 2011"
],
"url":"https://www.goodreads.com/book/show/6493208-the-immortal-life-of-henrietta-lacks"
}
],
"Class":"History",
"Description":[
{
"bookLabel":"The Rise and Fall of the Third Reich",
"shortLabel":"Rise and Fall",
"writer":"William L. Shirer",
"yearPublish":[
"1960"
],
"url":"https://www"
}
]
}
With MongoDB Java Driver v3.2.2 you can do something like this:
FindIterable<Document> iterable = collection.find(Document.parse("{\"Description.shortLabel\": {$regex: \"Cosmos\"}"));
This returns all documents containing Cosmos in the Description.shortLabel nested field. For an exact match, try this {"Description.shortLabel": "Cosmos"}. Replace shortLabel with bookLabelto search the bookLabel field. Then you can do iterable.forEach(new Block<Document>()) on the returned documents. To search both bookLabel and shortLabel, you can do a $or{}. My syntax could be wrong so check the MongoDB manual. But this is the general idea.
For this, you can use MongoDB's Text Search Capabilities. You'll have to create a text index on your collection for that.
First of all create a text index on your collection on fields bookLabel and shortLabel.
db.books.createIndex({ "Description.bookLabel" : "text", "Description.shortLabel" : "text" })
Note that this is done in the Mongo shell
Then
DBObject command = BasicDBObjectBuilder
.start("text", "books")
.append("search", "Cosmos")
.get();
CommandResult result = db.command(command);
BasicDBList results = (BasicDBList) result.get("results");
for(Object o : results) {
DBObject dbo = (DBObject) ((DBObject) o).get("obj");
String id = (String) dbo.get("_ID");
System.out.println(id);
}
Haven't really tested this. But just give it a try. Should work.
I have a bunch of code that uses the Apache Jena querybuilder API (SelectBuilder class). I am trying to add a term like this to my existing SPARQL query:
(?a ?b ?c) :hasMagicProperty ?this .
I have verified that this query works in TopBraid, but I can't figure out how to represent (?a, ?b, ?c) in the Jena API. What do I have to do to convert this list of Vars into a valid Jena resource node?
I am willing to explore alternate SPARQL-building frameworks, if they have robust support for typed literals, IRIs, and filters, as well as this list construct. I have skimmed over several other frameworks for building up SPARQL queries, but none of them seem to have a list construct.
Edit
My query building code (in Groovy) looks something like this:
def selectBuilder = new SelectBuilder()
selectBuilder.addPrefixes(...)
def thisVar = Var.alloc('this')
selectBuilder.addOptional(thisVar, 'rdf:type', ':MyEntity')
def aVar = Var.alloc('a')
def bVar = Var.alloc('b')
def cVar = Var.alloc('c')
List<Var> abc = [aVar, bVar, cVar]
//this doesn't work!!!
selectBuilder.addWhere(abc, ':hasMagicProperty', thisVar)
selectBuilder.addWhere(aVar, ':hasACode', 'code A')
selectBuilder.addWhere(bVar, ':hasBCode', 'code B')
selectBuilder.addWhere(cVar, ':hasCCode', 'code C')
def sparqlQuery = selectBuilder.buildString()
I have spent a couple of hours trying to work with the RDFList class, and I haven't figured it out. I'll keep trying, and see if I can grok it. In the meantime, any help would be appreciated. :)
Edit
Here is an unsuccessful attempt to use RDFList:
//this code does not work!
def varNode = NodeFactory.createVariable('a')
def model = ModelFactory.createDefaultModel()
def rdfNode = model.asRDFNode(varNode)
def rdfList = new RDFListImpl(model.createResource().asNode(), model)
//this line throws an exception!!
rdfList.add(rdfNode)
selectBuilder.addWhere(rdfList, ':hasMagicProperty', thisVar)
//com.hp.hpl.jena.shared.PropertyNotFoundException: http://www.w3.org/1999/02/22-rdf-syntax-ns#rest
The following method is a workaround, using multiple triples to recursively build up the RDF list:
/*
* Jena querybuilder does not yet support RDF lists. See:
* http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#collections
*/
private Node buildRdfCollection(SelectBuilder queryBuilder, List<?> itemList) {
if (itemList.isEmpty()) {
return RDF.nil.asNode()
}
def head = itemList.first()
def rest = buildRdfCollection(queryBuilder, itemList.subList(1, itemList.size()))
def listNode = NodeFactory.createAnon()
queryBuilder.addWhere(listNode, RDF.first, head)
queryBuilder.addWhere(listNode, RDF.rest, rest)
return listNode
}
...
def listNode = buildRdfCollection(queryBuilder, abc)
queryBuilder.addWhere(listNode, ':hasMagicProperty', thisVar)
The generated SPARQL code looks like this:
_:b0 rdf:first ?c ;
rdf:rest rdf:nil .
_:b1 rdf:first ?b ;
rdf:rest _:b0 .
_:b2 rdf:first ?a ;
rdf:rest _:b1 ;
:hasMagicProperty ?this .
This is a long-winded equivalent to:
(?a ?b ?c) :hasMagicProperty ?this .
I wrote the queryBuilder and I don't think that in it's current state it will do what you want. Query builder is based on (but does not yet fully implement) the w3c SPARQL 1.1 recommendation:
http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#rQuery
However, I think you can create your query using the Jena QueryFactory
String queryString = "SELECT * WHERE { "+
" OPTIONAL { ?this a :MyEntity } ."+
" (?a ?b ?c) :hasMagicProperty ?result . "+
" ?a :hasACode 'code A' . "+
" ?b :hasACode 'code B' . "+
" ?c :hasACode 'code C' ."+
" }";
Query query = QueryFactory.create(queryString) ;
Unfortunately, I don't think this is what you really want. Notice that ?this is not bound to any of the other statements and so will produce a cross product of all :MyEntity type subjects with the ?a, ?b, ?c and `?result`` bindings.
If you can create the query with QueryFactory, I can ensure that QueryBuilder will support it.
UPDATE
I have updated QueryBuilder (the next Snapshot should contain the changes). You should now be able to do the following:
Var aVar = Var.alloc('a')
Var bVar = Var.alloc('b')
Var cVar = Var.alloc('c')
selectBuilder.addWhere(selectBuilder.list(aVar, bVar, cVar), ':hasMagicProperty', thisVar)
selectBuilder.addWhere(aVar, ':hasACode', 'code A')
selectBuilder.addWhere(bVar, ':hasBCode', 'code B')
selectBuilder.addWhere(cVar, ':hasCCode', 'code C')
If you can also simply add the standard text versions of values in the list parameters like:
selectBuilder.list( "<a>", "?b", "'c'" )