簡體   English   中英

如何從Java中的MongoDB Atlas獲取特定字段?

[英]how to get specific fields from MongoDB Atlas in java?

以下Java代碼返回JSON對象,該對象包含MongoDB Atlas數據庫中“用戶”集合的所有字段,但是我只需要一個字段值,例如“ password”。 我該怎么辦?

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()投影(Projections.include( “密碼”))。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM