find in MongoCollection

后端 未结 5 1046
忘掉有多难
忘掉有多难 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:35

    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 collection = database.getCollection("mycoll");
    Document myDoc = collection.find(eq("_id", "test")).first();
    System.out.println(myDoc.toJson());
    

提交回复
热议问题