简体   繁体   English

如何将mongo db的列中的所有值检索到ArrayList中?

[英]How can I retrieve all the values in a column in mongo db into a ArrayList?

This is the values of my data stored in mongo db. 这是我存储在mongo db中的数据的值。 How am I able to retrieve all the data of "HomeTown" and store it all into a list? 如何获取“ HomeTown”的所有数据并将其存储到列表中? My list would contain AA, AA, BB, BB, etc... I want to use that array list to create a for loop of each Hometown. 我的列表将包含AA,AA,BB,BB等。我想使用该数组列表为每个故乡创建一个for循环。

Sample mongo data 样本mongo数据

 [{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ca"} , "HomeTown" : "AA" , 
"PhoneNumber" : { "CustName" : "xxx" , "Number" : "3403290"},
"MobileNumber" : { "CustName" : "yyy" , "Number" : "9323304302"}}]

    [{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ca"} , "HomeTown" : "AA" , 
"PhoneNumber" : { "CustName" : "xxx" , "Number" : "3403290"},
"MobileNumber" : { "CustName" : "yyy" , "Number" : "9323304302"}}]

    [{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ca"} , "HomeTown" : "BB" , 
"PhoneNumber" : { "CustName" : "xxx" , "Number" : "3403290"},
"MobileNumber" : { "CustName" : "yyy" , "Number" : "9323304302"}}]

    [{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ca"} , "HomeTown" : "BB" , 
"PhoneNumber" : { "CustName" : "xxx" , "Number" : "3403290"},
"MobileNumber" : { "CustName" : "yyy" , "Number" : "9323304302"}}]

How can I get all of the values of "HomeTown" in Java into an array? 如何将Java中所有“ HomeTown”的值都放入一个数组中? I am trying to create a for loop with the HomeTown Names. 我正在尝试使用HomeTown名称创建一个for循环。 I am currently using mongodb dependency through Spring boot. 我目前正在通过Spring Boot使用mongodb依赖关系。 I am not sure how would I implement mongodb into my java to use mongo db. 我不确定如何在我的Java中实现mongodb以使用mongo db。

Attempt/Problem 尝试/问题

I was able to retrieve mongodb values in a list using the following code. 我能够使用以下代码检索列表中的mongodb值。 I am trying to convert this list to a arraylist. 我正在尝试将此列表转换为arraylist。

public List<AppPortModel> getAppPortList() {
List<ApServerModel> objLst =  mongoOperations.findAll(ApServerModel.class);

String[] apServerArray = new String[objLst.size()];
for(int i = 0; i < objLst.size(); i++) {
            apServerArray[i] = objLst.get(i);
        }

Error on objLst.get(i) objLst.get(i)上的错误

Type mismatch: cannot convert from ApServerModel to String

Attempt #2 Following Sagar Example 在Sagar示例之后尝试#2

        @Autowired
    MongoOperations mongoOperations;

MongoCollection<ApServerModel> myCollection =   **mongoOperations.getCollection("apAllCustomer");**

List<ApServerModel> result = myCollection.find().into(new ArrayList<>());

Error on mongoOperations.getCollection mongoOperations.getCollection上的错误

Type mismatch: cannot convert from DBCollection to MongoCollection<ApServerModel>

Looks like you're using mongo 3.x driver.You'll need to use something like this. 看起来您正在使用mongo 3.x驱动程序。您需要使用类似这样的东西。

MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase("mkoydb");
MongoCollection<Document> myCollection = db.getCollection("apAllCustomer");
List<Document> result = myCollection.find().into(new ArrayList<>());

Fix for Attempt 1: 解决尝试1:

public List<AppPortModel> getAppPortList() {
   List<ApServerModel> objLst =  mongoOperations.findAll(ApServerModel.class);
   String[] apServerArray = new String[objLst.size()];
   for(int i = 0; i < objLst.size(); i++) {
        apServerArray[i] = objLst.get(i).getHomeTown();
}

Fix for Attempt 2: 解决尝试2:

 DBCollection dbCollection = mongoOperations.findAll(AppServreModel.class, "apAllCustomer");

You can call toArray() which, intuitively, returns a List<DBObject> . 您可以调用toArray() ,它直观地返回List<DBObject>

  List<DBObject> myList = null;
        DBCursor myCursor=myCollection.find(new BasicDBObject(),"HomeTown");
        myList = myCursor.toArray();

if you are using java driver with version 3.0 and above you can also do 如果您使用的Java驱动程序的版本为3.0及更高版本,则也可以

collection.find().projection(Projections.include("HomeTown"));

You can use projection to retrieve only required fields. 您可以使用投影仅检索必填字段。

db.apAllCustomer.find( ..., { HomeTown: 1 } );

In Java it depends on the API you use. 在Java中,它取决于您使用的API。 See this for Spring Data: 有关Spring数据,请参见以下内容:

SpringData MongoDB Using projection SpringData MongoDB使用投影

MongoDB-Java: MongoDB-Java:

Restrict fields in Result 限制结果中的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM