0

This is my example for Product MongoDB Collection:

product {

sku: abcQWE123,
url: www.xyz.com
comments:[
    "Lorem ipsum dolor sit amet",
    "inceptos himenaeos",
    "viverra. Aenean placerat ex in justo ul"
]

}

I want to get just a comment with "url" if it consist "ipsum".

Imagine that if database had a just entry which is above, my result would have been like this:

product{

url: www.xyz.com
comments:[
    "Lorem ipsum dolor sit amet",
]

}

I can get whole entries which contain "ipsum", using commands at below. But I want to be specific although I don't know how to do it. Could you help me about this subject?

public void main(String[] args) {

    MongoCursor<Product> p = mongoDB.getProducts().find(myMongoFilter("comments", "ipsum")).iterator();
    
    try {
        while (p.hasNext()) {
            System.out.println(p.next().toString());
        }
    } finally {
        p.close();
    }

}

public Document myMongoFilter(String myField, String myRegex) {
    return new Document(myField, new Document("$regex", myRegex));
}
2
  • You need the comments with "impsum" in it and the respective URL?? Commented Jun 8, 2021 at 16:44
  • @KaranGaur yes, I want to do it exactly. Commented Jun 8, 2021 at 17:31

2 Answers 2

1

Here is the my answer. @Sumanth229 thank you for mentioning the algorithm.

public static void main(String[] args) {
    ...
    ...
    List<Document> products = mongoDB.getProductsInRaw().aggregate(Filter.findComments("ipsum")).into(new ArrayList<>());

    products.forEach(document -> System.out.println(document.toJson()));
    ...
    ...
}

public MongoCollection<Products> getProducts() {
    return getDB().getCollection("products", Products.class);
}

public MongoCollection<Document> getProductsInRaw() {
    return getDB().getCollection("products");
}

public static ArrayList<Bson> findComments(String keywords) {
    ArrayList<Bson> queries = new ArrayList<>();
    queries.add(Filter.aggregateContains("comments", keywords));
    queries.add(Filter.aggregateUnwind("comments"));
    queries.add(Filter.aggregateContains("comments", keywords));

    //
    // To access full question's answer, remove comment block.
    // I don't want to use this line because of my requests were changed.
    // queries.add(new Document("$group", new Document("_id", new Document("comments", "$comments").append("url", "$url")).append("count", new Document("$sum", 1))));
    //
    //

    return queries;
}

public static Document aggregateContains(String field, String regex) {
    return new Document("$match", new Document(field, new Document("$regex", regex)));
}

public static Document aggregateUnwind(String field) {
    return new Document("$unwind", "$" + field);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can filter like this using projections in mongo

import com.mongodb.client.model.Projections;

MongoCursor<Product> p = mongoDB.getProducts()
                                .find(myMongoFilter("comments", "ipsum"))
                                .projection(Projections.include("comments", "url"))
                                .iterator();

3 Comments

Hi, your contribution does not working totally what I want to see. Yes, it masks the SKU field, but other comments do not go anywhere.
you have to unwind the comments list and then filter and group it back using MongoDB aggregation to show only the comments with ipsum in it.
If you have a code-solution, could you share with me? By the way, thank you for sharing algorithm, I am going to searching it that how to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.