Announcement Announcement Module
Collapse
No announcement yet.
MongoDB: Distinct Categories within Posts Collection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB: Distinct Categories within Posts Collection

    Did a google search and found a lot of non-Mongo responses. (Try explaining that sentence to someone in the 80's). Found this forum and can't find a definitive answer.

    Can we use "distinct" in the current spring data document API?

    Ie. I have a "posts" collection with entries similar to:

    {
    "_id" : ObjectId("4df02289cafacff33d560a2c"),
    "content" : "9 Lorem ipsum dolor sit amet, Excepteur sint occaecat cupidatat
    non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
    "dateCreated" : "06/08/2011 18:31 PM",
    "author" : "Tom Foolery",
    "categories" : [ "News of the 1800's", "Technology" ],
    "title" : "Review: 'uz200v2a758k' JS library.",
    "postId" : "f9be8697-dfc3-4a10-8965-5192e630ed09"
    }

    Trying to find all distinct "categories" entries, like:

    db.posts.distinct("categories");


    Thanks.

  • #2
    No? No one knows if this can be done, or should I provide a better explanation?

    Just looking for a way to get a distinct listing of a particular attribute. I know how to do this using Mongo syntax, but am trying to find an equivalent within the Spring Data world.

    Thanks.

    Comment


    • #3
      Originally posted by thoughtAddict View Post
      No? No one knows if this can be done, or should I provide a better explanation?

      Just looking for a way to get a distinct listing of a particular attribute. I know how to do this using Mongo syntax, but am trying to find an equivalent within the Spring Data world.

      Thanks.
      One way that I've been able to get a distinct listing is to use the MongoOperations.getCollection(<collectionName>) to get the DBCollection object and then use DBCollection.distinct(<key>)

      For example:

      We have a Collection UserLogin with:
      {
      username;
      timestamp;
      }

      and you want to get the distinct user names in the UserLogin collection via spring data, you could use:

      MongoOperations mongoOps = mongoTemplate;
      DBCollection userLoginCollection = mongoOps.getCollection("UserLogin");
      List distinctUserNames = userLoginCollection.distinct("username");

      Hope this helps.

      Comment


      • #4
        Was looking for the same thing. Couldn't find direct Spring Data support, but you can always use the Java Driver methods directly by getting the DBCollection from the mongoTemplate:
        DBCollection coll = mongoTemplate.getCollection("storeDetails");
        List<String> res = coll.distinct("zipCode");

        Comment

        Working...
        X