I have a collection named person with document like:
{
_id: 123456,
tags:["abc","abd","bcd"]
}
Now using Spring MongoTemplate I want to remove from tags where elements starts with ab, tried multiple ways but without luck.
For example, I tried but not working:
Update update = new Update().pull("tags",query(new Criteria().regex("^ab")));
Query query = query(where("_id").is(123456));
mongoTemplate.updateMulti(query, update, "person");
Related
I want to write a mongo query to fetch All Documents in a collection and project just 2 fields and no criteria.
I want to use streams here and iterate over the list of documents to process them.
How can I write this in Java Spring Mongo?
I have something like this:
public CloseableIterator<Employees> streamAllEmployees() {
Query query = new Query();
query.fields().include("_id.name","_id.dept");
query.addCriteria(Criteria.where("_id.dept").is(1));
return mongoOperations.stream(query, Employee.class);
}
but how can I change this to not have any QueryCriteria?
I have two Queries:
Query1(pagable) first datasource
Query2(pagable) second datasource
I would like to do something like this
Page results = merge(query1, query2)
How i can do this in JPA ?
Are both queries on the same entity? If so you can collect the content from both in a list (List<T> content) and build a new page object. Something as follows:
Page<T> mergedResult = new PageImpl(List<T> content);
https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/PageImpl.html
I have a list of entities on which I want to perform an update, I know I could update the table with list of String/Integer.. etc as the parameter with something like
#Query("update tableName i set i.isUpdated = true where i.id in :ids")
void markAsUpdated(#Param("ids") List<Integer> itemIds);
I'm trying to avoid repeated conversion of list of entities to list of Ids for making the query in DB. I know there are deleteAll and deleteInBatch commands which accept parameter as list of entities.
How do I do this in JPA Query, I tried the following but it didn't work yet.
#Modifying(flushAutomatically = true, clearAutomatically = true)
#Query("update tableName i set i.updated = true where i in :items")
void markAsUpdated(#Param("items") List<Item> items)
The query needs ids, it doesn't know how to deal with entities.
You have multiple options:
Just pass ids to the method, the client is responsible for extracting ids.
Pass entities and use SpEL for extracting ids
As suggested in the comments use a default method to offer both APIs and to delegate from one to the other.
As for the question that came up in the comments: You can move the method for extracting the id into a single method by either have relevant entities implement an interface similar to this one:
interface WithId {
Long getId();
}
Or by passing a lambda to the method, doing the conversion for a single entity:
List<ID> extractIds(List<E> entities, Function<E, ID> extractor) {
// ...
}
I have a Pojo with an attribute as
Class A{
#Id
#Field("item_id")
private String itemId;
}
When i try to update a document in MongoDB collection based on the itemId as below, it worked well and able to see from mongo ops logs that the query was transformed as "_id in itemIds "
Query query = new Query(Criteria.where("itemId").in(itemIds));
Update update = new Update();
update.set("field2", "abd");
mongoTemplate.updateMulti(query, update, A.class)
When i upgraded to spring-data-mongodb-2.1.5.RELEASE, the query i saw in the mongo logs was "item_id in itemIds". Since item_id is not a field and no index for that field in the collection, the query took forever to complete.
Any help to understand why the spring-data library is building the query as _id in older version and using the field as it is in newer version?
After a 2 minute search on the Spring documentation (https://docs.spring.io/spring-data/mongodb/docs/1.3.3.RELEASE/reference/html/mapping-chapter.html):
The following outlines what field will be mapped to the '_id' document field:
A field annotated with #Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field.
A field without an annotation but named id will be mapped to the '_id' field.
Did you try that already?
In Spring-data JPA, is there anyway to create a method query that is essentially searches by like??
I have the following method query
public MakeModel findByModelIgnoreCase(String model);
What I really want is a like expression. Do I need to just create a Criteria or a #Query annotation? Am I asking too much?
//Stupid example of what I want to be able to do
public MakeModel findByFuzzyModelIgnoreCase(String model);
Really I guess at the heart of it, I want to do a table search. I'm using Hibernate underneath Spring Data so I guess I could use some search api like Hibernate Search. I'm open to recommendations here.
If you don't want add "%" manually, you can use the following query methods:
MakeModel findByModelStartingWithIgnoreCase(String model); //SQL => LIKE 'model%'
MakeModel findByModelEndingWithIgnoreCase(String model); //SQL => LIKE '%model'
MakeModel findByModelContainingIgnoreCase(String model); //SQL => LIKE '%model%'
Like is supported too:
MakeModel findByModelLikeIgnoreCase(String model);
When you call the method use the follwing:
Add "%" at the start to say that it doesn't matter to be a strict start match ,
the same at the end, or you can use one of them it depends on the like you want.
MakeModel makeModel = findByModelLikeIgnoreCase("%"+model+"%");
if your make model Is test and the string to compare to is "%"+model+"%" then :
es is a match , T is a match , test is a match
the string to compare to is model+"%":
te is a match , es is not .