java - java - 如何从Java中的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.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);
}
}
推荐阅读
- reactjs - React app componentDidMount没有从父级获取道具
- ubuntu - SingleValueQuery 表达式在 pentaho 报表设计器 8.0 中不起作用
- pdf - 如何使用 pdfbox 锁定/解锁 pdf 中的图层
- aiml - 如何在 AIML 中设置具有默认值的变量?
- cmake - 从源代码编译 opencv_3.4.0 时出错
- email - Postfix 不会使用 DKIM 签名对所有邮件进行签名
- c++ - C++:STL数组的STL排序STL向量
- powershell-3.0 - 如何在 Powershell 数组中保存值
- java - 如何在java中的递归函数中增加变量
- java - Java中的多线程矩阵乘法。平均时间关闭。我是否正确使用了执行者?