Java 3.0 get distinct values from a column mongodb












2















I am really struggling here and have looked at other questions but just cant seem to get the answer I need.



What I am trying to do is pull through all the unique values of a column and then iterate through them and add them to an array. Ending up with the one column being stored in my array, but one of each value that exists not the multiple like there currently is.



Every time I try and do .distinct it asks me for the return class I have tried many different class but it just doesn't seem to work... Code is below any help would be appreciated.



 public static void MediaInteraction() {
//Storing data from MediaInteraction in MediaArray
//BasicDBObject Query = new BasicDBObject();
//Query.put("consumerid", "");



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", (What do I put here?)).iterator();

while (cursormedia.hasNext()) {
System.out.println(cursormedia.next());
MediasessionID.add(cursormedia.next());

}
System.out.println("Media Array Complete");
System.out.println(MediasessionID.size());
}









share|improve this question























  • what is collectionmedia a collection of? also what version of mongodb client are you using to check the usage..

    – nullpointer
    Dec 6 '16 at 11:09











  • It is a collection of application interactions, its a task given to me from my university. I need to pull out if the file the unique consumer id's, Excel already tells me out of the 500k rows I should get an answer of 1042 but I need to be able to do that through java linking it with the mongo collection. I use version 3.2.10.

    – User87797
    Dec 6 '16 at 11:13
















2















I am really struggling here and have looked at other questions but just cant seem to get the answer I need.



What I am trying to do is pull through all the unique values of a column and then iterate through them and add them to an array. Ending up with the one column being stored in my array, but one of each value that exists not the multiple like there currently is.



Every time I try and do .distinct it asks me for the return class I have tried many different class but it just doesn't seem to work... Code is below any help would be appreciated.



 public static void MediaInteraction() {
//Storing data from MediaInteraction in MediaArray
//BasicDBObject Query = new BasicDBObject();
//Query.put("consumerid", "");



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", (What do I put here?)).iterator();

while (cursormedia.hasNext()) {
System.out.println(cursormedia.next());
MediasessionID.add(cursormedia.next());

}
System.out.println("Media Array Complete");
System.out.println(MediasessionID.size());
}









share|improve this question























  • what is collectionmedia a collection of? also what version of mongodb client are you using to check the usage..

    – nullpointer
    Dec 6 '16 at 11:09











  • It is a collection of application interactions, its a task given to me from my university. I need to pull out if the file the unique consumer id's, Excel already tells me out of the 500k rows I should get an answer of 1042 but I need to be able to do that through java linking it with the mongo collection. I use version 3.2.10.

    – User87797
    Dec 6 '16 at 11:13














2












2








2








I am really struggling here and have looked at other questions but just cant seem to get the answer I need.



What I am trying to do is pull through all the unique values of a column and then iterate through them and add them to an array. Ending up with the one column being stored in my array, but one of each value that exists not the multiple like there currently is.



Every time I try and do .distinct it asks me for the return class I have tried many different class but it just doesn't seem to work... Code is below any help would be appreciated.



 public static void MediaInteraction() {
//Storing data from MediaInteraction in MediaArray
//BasicDBObject Query = new BasicDBObject();
//Query.put("consumerid", "");



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", (What do I put here?)).iterator();

while (cursormedia.hasNext()) {
System.out.println(cursormedia.next());
MediasessionID.add(cursormedia.next());

}
System.out.println("Media Array Complete");
System.out.println(MediasessionID.size());
}









share|improve this question














I am really struggling here and have looked at other questions but just cant seem to get the answer I need.



What I am trying to do is pull through all the unique values of a column and then iterate through them and add them to an array. Ending up with the one column being stored in my array, but one of each value that exists not the multiple like there currently is.



Every time I try and do .distinct it asks me for the return class I have tried many different class but it just doesn't seem to work... Code is below any help would be appreciated.



 public static void MediaInteraction() {
//Storing data from MediaInteraction in MediaArray
//BasicDBObject Query = new BasicDBObject();
//Query.put("consumerid", "");



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", (What do I put here?)).iterator();

while (cursormedia.hasNext()) {
System.out.println(cursormedia.next());
MediasessionID.add(cursormedia.next());

}
System.out.println("Media Array Complete");
System.out.println(MediasessionID.size());
}






java mongodb






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 6 '16 at 11:03









User87797User87797

335




335













  • what is collectionmedia a collection of? also what version of mongodb client are you using to check the usage..

    – nullpointer
    Dec 6 '16 at 11:09











  • It is a collection of application interactions, its a task given to me from my university. I need to pull out if the file the unique consumer id's, Excel already tells me out of the 500k rows I should get an answer of 1042 but I need to be able to do that through java linking it with the mongo collection. I use version 3.2.10.

    – User87797
    Dec 6 '16 at 11:13



















  • what is collectionmedia a collection of? also what version of mongodb client are you using to check the usage..

    – nullpointer
    Dec 6 '16 at 11:09











  • It is a collection of application interactions, its a task given to me from my university. I need to pull out if the file the unique consumer id's, Excel already tells me out of the 500k rows I should get an answer of 1042 but I need to be able to do that through java linking it with the mongo collection. I use version 3.2.10.

    – User87797
    Dec 6 '16 at 11:13

















what is collectionmedia a collection of? also what version of mongodb client are you using to check the usage..

– nullpointer
Dec 6 '16 at 11:09





what is collectionmedia a collection of? also what version of mongodb client are you using to check the usage..

– nullpointer
Dec 6 '16 at 11:09













It is a collection of application interactions, its a task given to me from my university. I need to pull out if the file the unique consumer id's, Excel already tells me out of the 500k rows I should get an answer of 1042 but I need to be able to do that through java linking it with the mongo collection. I use version 3.2.10.

– User87797
Dec 6 '16 at 11:13





It is a collection of application interactions, its a task given to me from my university. I need to pull out if the file the unique consumer id's, Excel already tells me out of the 500k rows I should get an answer of 1042 but I need to be able to do that through java linking it with the mongo collection. I use version 3.2.10.

– User87797
Dec 6 '16 at 11:13












1 Answer
1






active

oldest

votes


















2














The change that you probably want to introduce shall be somewhat like -



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here


Also from the docs -



/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param resultClass the class to cast any distinct items into.
* @param <TResult> the target type of the iterable.
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);


So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.



Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.






share|improve this answer





















  • 1





    Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

    – User87797
    Dec 6 '16 at 11:19













  • cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

    – nullpointer
    Dec 6 '16 at 11:21













  • of course! thank you!!

    – User87797
    Dec 6 '16 at 11:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40993793%2fjava-3-0-get-distinct-values-from-a-column-mongodb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The change that you probably want to introduce shall be somewhat like -



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here


Also from the docs -



/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param resultClass the class to cast any distinct items into.
* @param <TResult> the target type of the iterable.
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);


So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.



Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.






share|improve this answer





















  • 1





    Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

    – User87797
    Dec 6 '16 at 11:19













  • cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

    – nullpointer
    Dec 6 '16 at 11:21













  • of course! thank you!!

    – User87797
    Dec 6 '16 at 11:22
















2














The change that you probably want to introduce shall be somewhat like -



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here


Also from the docs -



/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param resultClass the class to cast any distinct items into.
* @param <TResult> the target type of the iterable.
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);


So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.



Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.






share|improve this answer





















  • 1





    Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

    – User87797
    Dec 6 '16 at 11:19













  • cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

    – nullpointer
    Dec 6 '16 at 11:21













  • of course! thank you!!

    – User87797
    Dec 6 '16 at 11:22














2












2








2







The change that you probably want to introduce shall be somewhat like -



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here


Also from the docs -



/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param resultClass the class to cast any distinct items into.
* @param <TResult> the target type of the iterable.
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);


So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.



Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.






share|improve this answer















The change that you probably want to introduce shall be somewhat like -



MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here


Also from the docs -



/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param resultClass the class to cast any distinct items into.
* @param <TResult> the target type of the iterable.
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);


So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.



Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 3:56









Mani

3231420




3231420










answered Dec 6 '16 at 11:14









nullpointernullpointer

45.3k1096185




45.3k1096185








  • 1





    Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

    – User87797
    Dec 6 '16 at 11:19













  • cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

    – nullpointer
    Dec 6 '16 at 11:21













  • of course! thank you!!

    – User87797
    Dec 6 '16 at 11:22














  • 1





    Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

    – User87797
    Dec 6 '16 at 11:19













  • cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

    – nullpointer
    Dec 6 '16 at 11:21













  • of course! thank you!!

    – User87797
    Dec 6 '16 at 11:22








1




1





Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

– User87797
Dec 6 '16 at 11:19







Thanks I used this and it worked this time: MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", String.class).iterator(); Thank you! Although it gives me a result of 521 and I know the result is 1042, so I am getting exactly 1/2, can you see any reason why this might be? Thanks!

– User87797
Dec 6 '16 at 11:19















cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

– nullpointer
Dec 6 '16 at 11:21







cursormedia.next() called twice in your logic. One inside the print statement. Just try and comment that out to see correct details.

– nullpointer
Dec 6 '16 at 11:21















of course! thank you!!

– User87797
Dec 6 '16 at 11:22





of course! thank you!!

– User87797
Dec 6 '16 at 11:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40993793%2fjava-3-0-get-distinct-values-from-a-column-mongodb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues