0

I'm trying to write a method that will check if a user_id is in the DB and then operate on the user_id.The portion of code that I'm stuck on is this:

DBObject query = new BasicDBObject(user_id,new BasicDBObject("$regex", true));
DBCursor result = dBcollection.find(query);             
if (result.equals("true")) {
    System.out.println("found");
    //do stuff
}
else{
//do other stuff
}

My database is set up this way:

{ "_id" : { "$oid" : "53b4443ad121894f16ea3699"} , "user_id" : "1683777896" , "countries" : { "JA" : 1}}
{ "_id" : { "$oid" : "53b4443ad121894f16ea369a"} , "user_id" : "453121657" , "countries" : { "TU" : 1}}

I want to be able to query on the user_id and then operate on the record associated with that user_id but I can't figure out the correct syntax in java for the "if" statement.

Stennie
  • 63,885
  • 14
  • 149
  • 175
adam
  • 940
  • 5
  • 13
  • 30

1 Answers1

2

Use this to query for the document with that user id:

DBObject query = new BasicDBObject("user_id", user_id);

Use this for the if statement to determine if the find actually found that user id:

if (result.hasNext())

If that doesn't go into the if statement, show us the code that gets the database and dBcollection, and make sure you are connecting to the right database and collection name.

lmyers
  • 2,654
  • 1
  • 24
  • 22