0

I am trying to query from my collection of documents which looks like:

{ "_id" : ObjectId("94"), "EmailAddress" :"[email protected]","Interests": "CZ1001,CE2004" }
{ "_id" : ObjectId("44"), "EmailAddress" :"[email protected]", "Interests":"CE1001,CE4002" }
{ "_id" : ObjectId("54"), "EmailAddress" :"[email protected]","Interests":"CE1001,CE2002" }

An example is that i want to retrieve the email addresses, given that the field "Interests" has that value i am looking for.

Example if i search CZ1001, i will get back Obj 1 EmailAddress details.

If i search CE1001, i will get back Obj 2 and Obj 3 EmailAddress details.

The object id are uniquely created when i inserted records at the start if that helps.

I am able to get the objects on MongoDB Shell using

db.users.find(Module: {"$regex": "CE1001"}})

Only the email addresses are needed.

I am trying to get all the email addresses and got stuck at this code.

Document doc = (Document) collection.find(new BasicDBObject("Module", {"$regex":"CE1001"})) .projection(Projections.fields(Projections.include("EmailAddress"), Projections.excludeId())).first(); 

Where new BasicDBObject("Module", {"$regex":"CE1001"}) is not allowed.

new BasicDBObject("Module", String_variable) is allowed

15
  • can't you store Interests as an array? Commented Oct 16, 2018 at 17:03
  • This a project that is done previously by others, i have no control over how it was stored at start. So i can't do that. Commented Oct 16, 2018 at 17:07
  • What do you mean by "in java"? Are you fetching all the documents and want to filter on results? What do you have so far? Commented Oct 16, 2018 at 17:14
  • Java as in, i want to retrieve those emails and from there proceed to send notifications alert to those emails. I am able to get the objects on MongoDB Shell using db.users.find(Module: {"$regex": "CE1001"}}) Commented Oct 16, 2018 at 17:18
  • 2
    Try new BasicDBObject("Module", new BasicDBObject("$regex", "CE1001")) Commented Oct 16, 2018 at 17:51

2 Answers 2

0

For a particular Interest, your mongoDB query would look like(taking CE1001 as an example):

db.collection.find({
  $or: [
    {
      Interests: {
        $regex: "^CE1001,"
      }
    },
    {
      Interests: {
        $regex: ",CE1001,"
      }
    },
    {
      Interests: {
        $regex: ",CE1001$"
      }
    }
  ]
},
{
  "EmailAddress": 1
})
Sign up to request clarification or add additional context in comments.

Comments

0

For others who happens to drop by this post. Below are the working codes. Credits to @Veeram.

FindIterable <Document> results = collection.find(new BasicDBObject("Module", new BasicDBObject("$regex", "CE1001")) .projection(Projections.fields(Projections.include("EmailAddress"), Projections.excludeId()));

for(Document doc : results) {
doc.getString("EmailAddress")
 }

Comments

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.