12

I have a MongoCollection<Document> in which I assign a collection. I'm trying to find a user by his id.

user = (Document) usersCollection.find(new Document("_id", username));

with that I'm getting an error

java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to org.bson.Document

When I try

    BasicDBObject query = new BasicDBObject(); 
    BasicDBObject fields = new BasicDBObject("_id", username);
    usersCollection.find(query, fields);

I'm getting an error

The method find(Bson, Class) in the type MongoCollection is not applicable for the arguments (BasicDBObject, BasicDBObject)

jimakos17
  • 905
  • 4
  • 14
  • 33

5 Answers5

18

Try to create a filter to pass to the find() method to get a subset of the documents in your collection. For example, to find the document for which the value of the _id field is test, you would do the following:

import static com.mongodb.client.model.Filters.*;

MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("mycoll");
Document myDoc = collection.find(eq("_id", "test")).first();
System.out.println(myDoc.toJson());
user_1330
  • 504
  • 1
  • 5
  • 22
chridam
  • 100,957
  • 23
  • 236
  • 235
1

Your issue is that you assume that the find() method returns a single Document. It doesn't. It returns a list of them.

In MongoDB 2 Java driver there was a method on the DBCollection class named findOne(). In the MongoDB 3 Java driver API, the findOne() method isn't there. So your new code for finding exactly one document becomes similar too this one:

collection.find(eq("_id", 3)).first()

where eq("_id", 3) is called a filter on your collection.

aahoogendoorn
  • 444
  • 6
  • 9
0
MongoCollection<Document> filterCriteriaDoc = mongoDatabase.getCollection("documentName");

Document filterDoc = new Document();

filterDoc.put("col1", "value");

filterDoc.append("col2", "value");

filterDoc.append("col2", "value");

Iterator<Document> iter = filterCriteriaDoc.find(filterDoc).iterator(); 

iter.next() can give you document .

heilerich
  • 944
  • 8
  • 14
Minal
  • 1
  • 2
-1

If you using IP to connect to MongoDb here how you do it change HEREYOURIP

import static com.mongodb.client.model.Filters.eq;

public static Document GetDocumentFromDataBase(String dataBase,String DBcollection,String field, String value) {
MongoClient mongoClient = new MongoClient(  " HEREYOURIP ",27017 );
MongoDatabase database =  mongoClient.getDatabase(dataBase);
MongoCollection<Document> collection = database.getCollection(DBcollection);
Document myDoc = collection.find(eq(field, value)).first();
    mongoClient.close();
    return myDoc;}

edited found other way do it

public static String GetFromDB(String DATABASE_NAME,String collectionName, String field, String value) {
        String valueBack;
        BasicDBObject whereQuery = new BasicDBObject();
        whereQuery.put("_id", new ObjectId(value));
        
        MongoClient mongoClient = new MongoClient(System.getenv("HERE_YOUR_DB_IP"), 27017);
        MongoDatabase database = mongoClient.getDatabase(DATABASE_NAME);
        MongoCollection<Document> collection = database.getCollection(collectionName);
        Document myDoc = collection.find(whereQuery).first();
        if (myDoc != null) {

            valueBack = myDoc.toString();
            mongoClient.close();
            return valueBack;
        }
        mongoClient.close();
        return null;

    }
Vladi
  • 1,662
  • 19
  • 30
-2

Do this -

 MongoClient client = new MongoClient();
    DBObject resultObject  = new BasicDBObject("_id", username);
    MongoDatabase database = client.getDatabase("DBNAME");
    MongoCollection<Document> collection =  database.getCollection("COLLECTION_NAME");
    DBObject dbObject  = new BasicDBObject("_id", username);
    resultObject   = collection.find(dbObject).next();
    String result =  resultObject.get(YOUR_COLOUM_NAME);
Amit Das
  • 1,077
  • 5
  • 17
  • 44