$or operator with multiple expressions and multiple fields in an expression using Spring Data Reactive MonogoDB - mongodb

In MongoDB, I can use $or[{key1:'value11', key2:'value12'}, {key1:'value21', key2:'value22'}, {key1:'value31', key2:'value32'}, ...] to query several documents which matches at least one of the expressions in the $or operator. Then how the thing can be done using Spring Data Reactive MonogoDB?
In particular, I define a entity class as:
#Document
public class MyDocument
{
#Id
private String id;
private String field1;
private String field2;
}
And then, the repository interface for the entity:
public interface MyDocumentRepository extends ReactiveMongoRepository<MyDocument, String>
The question now is how to define a method in MyDocumentRepository to query the documents with field1 and field2:
There seems no proper keywords to create a query method (findAllBy(field1AndField2)In???)
If using JSON-based Query Methods, I really do know how to complete the Cloze test...
#Query("{$or:[(:fields)]}
Flux<MyDocument> findAllBy????(Flux<???> fields)

Spring Data MongoDB has support for ReactiveMongoTemplate. In a repository, you can use this as a connection to MongoDB which can be used with #Autowire.
In ReactiveMongoTemplate you can create Criteria with and and or operation like
Query query = new Query();
query.addCriteria(
new Criteria().andOperator(
Criteria.where("field1").exists(true),
Criteria.where("field1").ne(false)
)
);
and this can be passed to MongoDB with the before created instance of ReactiveMongoTemplate
Flux<Foo> result = reactiveMongoTemplate.find(query, Foo.class);
Documentation for use of configuration of ReactiveMongoTemplate if needed can be found here

Related

Java Spring Data MongoDB

I am using Spring data mongo with azure cosmos. My structure looks like below.
I have an Id field in my collection that is not annotated with #Id. I see both _id and id are in the DB but When I retrieve id field comes with the value is in _id.
#Document(collection = "mycollection")
class MyObject{
private String id;
...
}
public interface MyRepository extends MongoRepository<MyObject, Void> {
}
Used #Field("id") to tell spring data threat this field as is not as _id/pk field for mongo

How to add default criteria to all the queries by default in mongo spring boot

I have a requirement to soft delete documents in a given MongoDB collection. For that, I use a boolean called deleted. So now when I am retrieving data from the database, I have to always mention taking the data where the deleted=false.
Eg:
public Organization findOrgById(String id) {
Query query = new Query();
Criteria criteria = Criteria.where(Constants.ENTITY_ID).is(id)
.and(Constants.DELETED).is(false);
query.addCriteria(criteria);
Organization res = mongoTemplate.findOne(query, Organization.class);
return res;
}
Is there a way to specify that always all the criteria to add deleted=false by default without mentioning it in the code itself?
In Hibernate core there is an annotation #Where but it is not working with mongo documents.
I think the best way to do this is extend the MongoTemplate class that will add your deleted=false condition to all Find queries.
Here is an example of how to do it with one method used to execute findOne queries:
public class ExtendedMongoTemplate extends MongoTemplate {
private static final Document DELETED_CRITERIA_DOC = Criteria.where(Constants.DELETED).is(false)
.getCriteriaObject();
#Override
protected <T> T doFindOne(
String collectionName,
Document query,
Document fields,
CursorPreparer preparer,
Class<T> entityClass) {
query.putAll(DELETED_CRITERIA_DOC);
return super.doFindOne(collectionName, query, fields, preparer, entityClass);
}
...
}
This method is called in the method doFindOne(Query query, Class<?> entityClass) (the last one delegates executing
Other methods to override are:
protected <S, T> List<T> doFind(String collectionName, Document query, Document fields,
Class<S> entityClass, CursorPreparer preparer);
protected <T> T doFindAndRemove(String collectionName, Document query, Document fields,
Document sort, #Nullable Collation collation, Class<T> entityClass);
protected <T> T doFindAndModify(String collectionName, Document query, Document fields, Document sort,
Class<T> entityClass, UpdateDefinition update, #Nullable FindAndModifyOptions options);
protected <T> T doFindAndReplace(String collectionName, Document mappedQuery, Document mappedFields,
Document mappedSort, com.mongodb.client.model.Collation collation, Class<?> entityType,
Document replacement, FindAndReplaceOptions options, Class<T> resultType);
These methods execute queries at low-level, so they accept BSON-documents with the query criteria, not Spring's criteria. If you do this, the Find-methods will add an additional criteria to all you queries.
You also can override methods findOne, find, findAndModify and so on in a similar manner, but there are a lot of these methods that all use doFind* methods. Thus overriding doFind* will lead to work with all Find-queries. And don't forget override also findById (it also uses doFindOne internally).
By the way, #Where annotation is from Hibernate, but Spring Data Mongo doesn't use them. It requires its own annotations to work with your entities.

Spring Data - Mongo DB - #TextIndexed over #DBRef

Is it possible somehow to search in String fields over #DBRef.
I have this #Document:
public class DocumentFileVersion {
#TextIndexed
#DBRef
private OtherObject otherObject
and I will search in String fields of otherObject. Is there any possibility to do that?
DBRef are designed to be queried by id reference only.
So it is not possible. You should rethink your schema structure.

Nested query in mongodb using spring data

I am trying to use nested Mongodb query but it does not work.
It is similar to Spring data mongodb query for subdocument field
But suggestions mentioned there does not work.
Please find my documents below.
#Document
public class Ticket {
#Id
private String id;
#DBRef
#CascadeSave
private Customer customer;
// getters and setters
}
#Document
public class Customer {
#Id
private String id;
private String firstName;
// getters and setters
}
public interface TicketRepository extends MongoRepository<Ticket, String> {
public List<Ticket> findByCustomerFirstName(String firstName);
}
I tried both findByCustomerFirstName and findByCustomer_FirstName but it does not work. Any suggestions ?
These suggestions are right it should work...
Official docs explains it as you did it:http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.query-property-expressions
Property expressions can refer only to a direct property of the
managed entity, as shown in the preceding example. At query creation
time you already make sure that the parsed property is a property of
the managed domain class. However, you can also define constraints by
traversing nested properties. Assume a Person has an Address with a
ZipCode. In that case a method name of
List<Person> findByAddressZipCode(ZipCode zipCode);
creates the
property traversal x.address.zipCode
Just one thing, remove #Document from Customer and try it, Mongodb didn't support join queries (I'm not sure if now it does)... so you're document should be Ticket and it must have a embbebed document Customer as a inner object and not in a different document.

How can I query to find mongo entities whose list of sub-entities contain a field matching a string?

I have a collection of entities that look like this:
public class ClientEntity {
#Id
private String id;
#Indexed(unique = true)
private String clientId;
private String name;
#DBRef
private List<ClientMachineEntity> machines;
...
}
...where ClientMachineEntity looks like:
public class ClientMachineEntity {
#Id
private String id;
#Indexed(unique = true)
private String clientMachineId;
private String hostName;
...
}
I have a working search that finds ClientEntities by matching against "clientId" and "name":
public List<ClientEntity> searchByIdAndName(String id, String name) {
Criteria idCriteria = Criteria.where("clientId").regex(id, "i");
Criteria nameCriteria = Criteria.where("name").regex(name, "i");
Query query = new Query(new Criteria().orOperator(idCriteria, nameCriteria));
...
}
So my question is, how can I expand this search so that it also matches against "clientMachineId" in the list of sub-entities? I tried adding the following criteria:
Criteria machineCriteria = Criteria.where("machines.clientMachineId").regex(id, "i");
...but that doesn't work, presumably because machines is a LIST of entities, not just a single sub-entity.
UPDATE: It seems like what I'm looking for is the .elemMatch() functionality, but when I try that:
Criteria machineCriteria = Criteria.where("machines").elemMatch(Criteria.where("clientMachineId").regex(id, "i"));
...I get the following error:
org.springframework.data.mapping.model.MappingException: No mapping metadata found for class com.mongodb.BasicDBObject
You can't query by fields in subentities linked with DBRef. If ClientMachineEntity would be embedded in ClientMachine - then you could use dot notation or $elemMatch depending on needs.
In your particular example - couldn't field ClientMachineEntity.clientMachineId be saved as _id and used as a primary key? Then you could get the results you need - take a look at: How to query mongodb with DBRef
My suggestion for development with Spring Data MongoDB is - first learn how to (and if it's possible) do it in plain Javascript with MongoDB console, then learn how to do the same with Spring Data MongoDB.