Build dynamic queries with Spring Data MongoDB Criteria

24,788

Solution 1

Doesn't this work for you?

Criteria criteria = Criteria.where("contentType").is("application/vnd.sometype");

List<Criteria> docCriterias = new ArrayList<Criteria>(docs.size());

for (Document doc: docs) {
    docCriterias.add(Criteria.where("metadata.name").is(doc.getName())
                               .and("metadata.version").is(doc.getVersion()));
}

criteria = criteria.orOperator(docCriterias.toArray(new Criteria[docs.size()]));

?

Solution 2

Here we need to build new query and embed the criteria to the built new query. And also, we have to create a list of criteria using some criteria for embed to the query. Here my example is providing a list of metadata and we don't know the name of parameter which will send for us. So, The solution is as given follow.

List<Criteria> criterias = new ArrayList<>();

    for (MetaData metaData : newDoc.getMetaData()) {
        Criteria dynamicCriteria = Criteria.where("metaData.key").is(metaData.getKey()).andOperator(Criteria.where("metaData.value").is(metaData.getValue()));
        criterias.add(dynamicCriteria);
    }

    Criteria criteria = new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()]));

    Query searchQuery = new Query(criteria);

    List<Document> documents = mongoTemplate.find(searchQuery, Document.class);
Share:
24,788
Jamie Cramb
Author by

Jamie Cramb

Updated on July 09, 2022

Comments

  • Jamie Cramb
    Jamie Cramb almost 2 years

    I would like to run a bulk delete operation on a list of documents in MongoDB that have been selected by the user in the UI so I need to dynamically build a query that looks like the following (the or clause expands for every document selected):

    {
        $and: [
            {
                "contentType": "application/vnd.sometype"
            },
            {
                $or: [
                    {
                        "metadata.name": "someName",
                        "metadata.version": "someVersion"
                    },
                    {
                        "metadata.name": "someOtherName",
                        "metadata.version": "someOtherVersion"
                    }
                ]
            }
        ]
    },
    Fields: null,
    Sort: null
    

    Just now I'm using string concatenation to achieve this.

    Is it possible to build this query with the Spring Data MongoDB Criteria Builder (org.springframework.data.mongodb.core.query.Criteria)?

  • Ramzan Zafar
    Ramzan Zafar over 9 years
    @Artem Bilan criteria.orOperator(docCriterias.toArray(new Criteria[docs.size()])); im getting issue while converting my Criteria list to an Array its keep saying that orOperator not defined for Criteria[]? can you plese help?
  • Artem Bilan
    Artem Bilan over 9 years
    Please, check your Spring Data MongoDB version. Try to use the latest one.
  • Ramzan Zafar
    Ramzan Zafar over 9 years
    I'm using 1.5.0.RELEASE
  • Ramzan Zafar
    Ramzan Zafar over 9 years
    its keep saying The method orOperator(Criteria...) in the type Criteria is not applicable for the arguments (Criteria[])
  • Artem Bilan
    Artem Bilan over 9 years
    Don't you use org.hibernate.Criteria by mistake?
  • Ramzan Zafar
    Ramzan Zafar over 9 years
  • Prabjot Singh
    Prabjot Singh over 8 years
    What will be execution order of multiple criteria ?
  • Half Blood Prince
    Half Blood Prince almost 8 years
    How to print this criteria? I want to see the actual query.
  • Artem Bilan
    Artem Bilan almost 8 years
    See Criteria.getCriteriaObject().toString(), where the last one does exactly this: JSON.serialize(this);