如何在java中从MongoDB Atlas获取特定字段?

如何在java中从MongoDB Atlas获取特定字段?,java,json,mongodb,mongodb-atlas,Java,Json,Mongodb,Mongodb Atlas,下面的Java代码返回JSON对象,该对象包含MongoDB Atlas数据库中用户集合的所有字段,但我只需要一个字段值,比如密码。我该怎么办 package com.servlets; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.Mongo

下面的Java代码返回JSON对象,该对象包含MongoDB Atlas数据库中用户集合的所有字段,但我只需要一个字段值,比如密码。我该怎么办

package com.servlets;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoException;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
public class mongoDB 
{
    public static void main(String[] args)
    {
        String username = "gary29198";
        Gson gson = new Gson();
        MongoClientURI uri = new MongoClientURI("mongodb+srv://gary29198:0001221149@sih2019-yp3zc.mongodb.net/test?retryWrites=true");
        MongoClient mongoClient = new MongoClient(uri);
        MongoDatabase database = mongoClient.getDatabase("SIH2019");
        MongoCollection<Document> collections = database.getCollection("users");
        FindIterable<Document> find = collections.find(Filters.eq("username", username));
        try( MongoCursor<Document> cursor = find.iterator() ) 
        {
            while(cursor.hasNext())
            {
                System.out.println(gson.toJson(cursor.next()));
            }
        }
        catch(MongoException e)
        {
            System.out.println(e.getMessage());
        }
    }

}
请参见此示例:

package com.jcg.java.mongodb;
import org.apache.log4j.Logger;
import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class MongoDemo {

    private static Logger log = Logger.getLogger(MongoDemo.class);

    // Fetching all documents from the mongo collection.
    private static void getAllDocuments(MongoCollection<Document> col) {
        log.info("Fetching all documents from the collection");

        // Performing a read operation on the collection.
        FindIterable<Document> fi = col.find();
        MongoCursor<Document> cursor = fi.iterator();
        try {
            while(cursor.hasNext()) {
                log.info(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
    }

    // Fetch a selective document from the mongo collection.
    private static void getSelectiveDocument(MongoCollection<Document> col) {
        log.info("Fetching a particular document from the collection");

        // Performing a read operation on the collection.
        String col_name = "name", srch_string = "Charlotte Neil";
        FindIterable<Document> fi = col.find(Filters.eq(col_name, srch_string));        
        MongoCursor<Document> cursor = fi.iterator();
        try {
            while(cursor.hasNext()) {
                log.info(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
    }

    @SuppressWarnings("resource")
    public static void main(String[] args) {

        // Mongodb initialization parameters.
        int port_no = 27017;
        String host_name = "localhost", db_name = "sampledb", db_coll_name = "emp";

        // Mongodb connection string.
        String client_url = "mongodb://" + host_name + ":" + port_no + "/" + db_name;
        MongoClientURI uri = new MongoClientURI(client_url);

        // Connecting to the mongodb server using the given client uri.
        MongoClient mongo_client = new MongoClient(uri);

        // Fetching the database from the mongodb.
        MongoDatabase db = mongo_client.getDatabase(db_name);

        // Fetching the collection from the mongodb.
        MongoCollection<Document> coll = db.getCollection(db_coll_name);

        // Fetching all the documents from the mongodb.
        getAllDocuments(coll);

        log.info("\n");

        // Fetching a single document from the mongodb based on a search_string.
        getSelectiveDocument(coll);
    }
}
可以使用投影

collection.find.projectionProjections.includepassword


这就是我所做的,但我想这样做:从username=gary29198的用户那里选择密码。谢谢@lonwolf,它工作得很好。我知道投影的概念,但无法得到它的正确语法和用法…可能是重复的