0

I'm trying ask if a field exist inside a document inside another document in an mongodb database using java driver. The field that I asking for is idMovie.

For example, I expect that it print in the console "idMovie value already exist" if idMovie is 2, for example. Or print "not exist" if idMovie is 10, because 10 are not in the document

this is my bson object:

 { "_id" : { "$oid" : "5320aa3c3e468eaeb52dccdc"}, 
   "document" : "movies" , 
   "rateMovies" : [ 
        { 
         "idMovie" : 2 , 
         "average" : "0" , 
         "sum" : "0" , 
         "total" : "0"
        } , 

       { 
         "idMovie" : 3 ,
         "average" : "0" ,
         "sum" : "0" , 
         "total" : "0"
       }]
  } 

And this is my java code:

MongoClient mongo = new MongoClient ("localhost", 27017);
        DB db = mongo.getDB("DBHC2");
        DBCollection movieDocument = db.getCollection("rateMovies");

        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("idMovie", idMovie);
        DBCursor cursor = movieDocument.find(searchQuery);

        /* if is true, the idMovie value already exist in database. */
        if(cursor.hasNext()){
            System.out.println("idMovie value already exist");
        }
        else{
            System.out.println("not exist");
        }

, but don't works for my. Any help is good received, thank you!!

Community
  • 1
  • 1
hcarrasko
  • 2,320
  • 6
  • 33
  • 45
  • _"but don't works"_ is not sufficient. HOW does it not work? What are you seeing that is unexpected? Have you stepped through the code in an IDE debugger? What did that tell you? – Jim Garrison Mar 12 '14 at 19:12
  • well, if you follow the java code, I expect that it print in the console "idMovie value already exist" if idMovie is 2, for example. Or print "not exist" if idMovie is 10 because 10 are not in the document. Thanks. – hcarrasko Mar 12 '14 at 19:16
  • Can you confirm if the name of your collection is "rateMovies"? – Anand Jayabalan Mar 12 '14 at 19:32
  • yes the name is correct – hcarrasko Mar 12 '14 at 19:36
  • Can you please post the output of db.rateMovies.findOne() from the shell? – Anand Jayabalan Mar 12 '14 at 19:39
  • sorry, you have reason, the collection name is movies, but if I replace `DBCollection movieDocument = db.getCollection("movies");` for `DBCollection movieDocument = db.getCollection("rateMovies");` I steel have the same result. – hcarrasko Mar 12 '14 at 19:42

2 Answers2

1

Thanks for you help, but I find the answer by my self.

        MongoClient mongo = new MongoClient ("localhost", 27017);
        DB db = mongo.getDB("DBHC2");
        DBCollection movieDocument = db.getCollection("movies");

        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("rateMovies.idMovie", idMovie);
        DBCursor cursor = movieDocument.find(searchQuery);
hcarrasko
  • 2,320
  • 6
  • 33
  • 45
0

Try this:

    searchQuery.put("idMovie", new BasicDBObject("$exists",true));
Anand Jayabalan
  • 12,294
  • 5
  • 41
  • 52
  • if you follow the java code, I expect that it print in the console "idMovie value already exist" if idMovie is 2, for example. Or print "not exist" if idMovie is 10, because 10 are not in the document. Thanks. – hcarrasko Mar 12 '14 at 19:19