find in MongoCollection

后端 未结 5 1052
忘掉有多难
忘掉有多难 2020-12-20 13:54

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

user = (Document) usersCollection         


        
5条回答
  •  悲哀的现实
    2020-12-20 14:21

    Do this -

     MongoClient client = new MongoClient();
        DBObject resultObject  = new BasicDBObject("_id", username);
        MongoDatabase database = client.getDatabase("DBNAME");
        MongoCollection collection =  database.getCollection("COLLECTION_NAME");
        DBObject dbObject  = new BasicDBObject("_id", username);
        resultObject   = collection.find(dbObject).next();
        String result =  resultObject.get(YOUR_COLOUM_NAME);
    

提交回复
热议问题