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));
}