Filtering out document references in MongoDB

18 views Asked by At
@Entity
public class Node {
    @Id
    private String id;
    @Version
    private String vId;
    private String nodeType;
    private Object simpleValue;

    @DocumentReference
    private List<Node> complexNodes;
    Map<String, List<String>> complexNodeTypeVsAssociatedIds;
}
{
    "_id": {
      "$oid": "65f44309da8c4668e9670733"
    },
    "nodeType": "projectGroup",
    "complexNodes": [
      {
        "$oid": "65f44302da8c4668e967064d"
      },
      {
        "$oid": "65f44303da8c4668e9670658"
      },
      {
        "$oid": "65f44303da8c4668e9670663"
      }
    ],
    "complexNodeTypeVsAssociatedIds": {
      "measure": [
        "65f44302da8c4668e967064d",
        "65f44303da8c4668e9670658",
        "65f44303da8c4668e9670663"
      ]
    },
    "_class": "com.test.Node"
  }

Above is a MongoDB entity and the document. 'complexNodes' are stored as Document references which are again of type 'Node'.

I want to filter the complexNode(s) from the Node document with the input complexNodes Ids.

Example - I am given with Id - 65f44309da8c4668e9670733 and set of Ids - [65f44302da8c4668e967064d, 65f44303da8c4668e9670658]. The output should be the Document with Id - 65f44309da8c4668e9670733 and should contain complexNodes with Id - 65f44302da8c4668e967064d and 65f44303da8c4668e9670658 only.

I tried with below, but it didn't work.

public List<Node> findNodesByMeasureIds(String pgReference, List<String> measureIds) {
        Criteria criteria = new Criteria()
            .and("_id").is(pgReference)
            .and("complexNodes.$id").in(measureIds);
        
        Query query = new Query(criteria);
        
        return mongoTemplate.find(query, Node.class);
    }
0

There are 0 answers