I have a MongoCollection
in which I assign a collection.
I\'m trying to find a user by his id.
user = (Document) usersCollection
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.
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);
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;
}
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 .
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());