JayC
JayC

Reputation: 2292

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. How am I able to retrieve all the data of "HomeTown" and store it all into a list? My list would contain AA, AA, BB, BB, etc... I want to use that array list to create a for loop of each Hometown.

Sample mongo data

 [{ "_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? I am trying to create a for loop with the HomeTown Names. I am currently using mongodb dependency through Spring boot. I am not sure how would I implement mongodb into my java to use mongo db.

Attempt/Problem

I was able to retrieve mongodb values in a list using the following code. I am trying to convert this list to a 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)

Type mismatch: cannot convert from ApServerModel to String

Attempt #2 Following Sagar Example

        @Autowired
    MongoOperations mongoOperations;

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

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

Error on mongoOperations.getCollection

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

Upvotes: 0

Views: 1526

Answers (3)

s7vr
s7vr

Reputation: 75934

Looks like you're using mongo 3.x driver.You'll need to use something like this.

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:

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:

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

Upvotes: 1

lexicore
lexicore

Reputation: 43661

You can use projection to retrieve only required fields.

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

In Java it depends on the API you use. See this for Spring Data:

SpringData MongoDB Using projection

MongoDB-Java:

Restrict fields in Result

Upvotes: 0

satish chennupati
satish chennupati

Reputation: 2650

You can call toArray() which, intuitively, returns a 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

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

Upvotes: 0

Related Questions