-1

Issue in querying the embedded document in mongo db. I am trying to get the conversationId for the users but it returning null.

Sample document:

{
"_id" : ObjectId("5787391f191fda3a4430c749"),
"conversationId" : "fWFGIr0xAbQytmVcQIPV",
"user" : [{_id : "800", "name" : "Tim"},{_id : "500", "name" : "Kingsley"},
{_id : "400", "name" : "Roger"}],
"type" : "PRIVATE"
}

Query:

Aggregation agg = newAggregation(
            match(Criteria.where("type").is("PRIVATE")),
            group("conversationId").push("user.id").as("users"),
            match(Criteria.where("users").all(Arrays.asList('800','400','500')))
            );
    AggregationResults<Rooms> groupResults = mongoOps.aggregate(agg, ROOMS, Rooms.class);
      List<Rooms> result = groupResults.getMappedResults();
      result.get(0).getId() // returns null

The result.get(0).getId() is returning null, as per my query I am expecting the conversation Id that is present for the users.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Karthik
  • 470
  • 8
  • 25

1 Answers1

1

I am not sure, why you are grouping on conversationId and then creating a set with user's id who took part in that conversation.

If your desired result is to find a conversation in which the users '800', '400' and '500', then you can straight away use find query with $all.

Mongo Query will look like this:

db.test.find({"user._id":{"$all":["800","500","400"]}})
Nattyk
  • 138
  • 1
  • 13
  • Yeah my desired result is like that only. But, its not finding the desired conversation Id – Karthik Jul 14 '16 at 13:40
  • @Karthik: You tried with the above query? can you paste that java code? – Nattyk Jul 14 '16 at 14:28
  • Aggregation agg = newAggregation( match(Criteria.where("type").is("PRIVATE")), group("conversationId").push("user._id").as("users"), match(Criteria.where("users").all(userProfileIds)) – Karthik Jul 14 '16 at 14:30
  • @Karthik You are once again doing the grouping. As per your statement, you don't need to aggregate too. So can you just try 'find' query for the mongo query given above? – Nattyk Jul 14 '16 at 15:43
  • Its working , I have some other issue https://stackoverflow.com/questions/38395261/getting-com-mongodb-mongoexceptionduplicatekey-in-mongodb-with-java-using-upser – Karthik Jul 15 '16 at 11:44