Skip to main content
Version: v5.2

findWithRelatedGraph

findWithRelatedGraph query​

Use the $findWithRelatedGraph query to look up related items across multiple named user collections, hopping from related item to related item by relationship. You can query a graph data structure in the following ways:

  • Hop from parent to child related items
  • Inversely hop from child to parent related items

These directions of travel enable you to query anywhere in a relationship hierarchy as far as the path of relationships exists.

The following figure shows this flexibility: the path inversely travels from a child related item to a parent related item. The path then continues to a different child related item of the parent, and continues to that related item’s child item.

Figure: Path traversing against and with a directional relationship image

findWithRelatedGraph use cases​

You can use the $findWithRelatedGraph query in the two following use cases:

In both use cases, in your query, you can define the information you require on any node in the path.

findWithRelatedGraph request structure and fields​

For more information on how to structure a $findWithRelatedGraph query request, see the following example code structure with reference to Table 1.

Your use case determines the request properties you use. For more information, see the following request types:


{
"$findWithRelatedGraph": {
"start": {
"collectionDesc": {
"<property to identify start node collection by>": "<value>"
},
"query": {
"_id": "<start node id value>"
},
"options": {
"page": {
"_offSet": 0
"_pageSize": 25
},
"project": {
"<start node property you want to appear in response>": 1
},
"sort": {
"<start node property you want to sort>": 1
}
}
},
"to": {
"segments": [
{
"relatedDesc": {
"<RelatedItem property to identify it by>": "<value>"
},
"from": "<name of a previous hop>", //for path building only
"as": "<give this path segment a name>",
"minDepth": 1, //for path discovery only
"maxDepth": 1, //for path discovery only
"options": {
"project": {
"<relatedItem property to appear in response>": 1
},
}
}
],
"as": "<property name for the path query results in the response>",
"response": "path",
"options": {
"page": {
"_offSet": 0
"_pageSize": 25
},
}
}
}
}

Table 1: findWithRelatedGraph fields table

FieldTypeRequiredDescription
startRequiredObjectPass an object with the following look up criteria for the start node collections and related items to this object: "collectionDesc", "collectionProject", "query", "options"
start.collectionDescRequiredObjectAdd a property and value to identify the named user collection, such as an id, user item id, or user type property.
start.collectionDesc._versions.allOptionalBooleanSet to true to query all named user collection versions. If you do not add this property, only the tip version is queried.
start.collectionDesc._versionsObjectOptionalEnter a valid query to filter properties by version. start.collectionDesc._versions.all must be set to true when using this filter.
start.collectionProjectOptionalObjectEnable or disable the named user collection fields in the response with simple query projection.
start.queryNoObjectAdd the start node id property and value.
start.optionsNoObjectAdd projection and page options for the collection response.
start.options.pageNoObjectSet the response pagination options, such as _offset and _pageSize. The default value for _offset is 0 and for _pageSize is 25.
start.options.projectNoObjectEnable or disable the related items fields in the response with simple query projection. To enable a field, enter the field as a property and the value as 1.
start.options.sortNoObjectSort the results in the response based on the related items fields with a simple query sort.
toRequiredObjectAdd the look up criteria for the path, from the start node to the end node.
to.segmentsRequiredArrayAdd an array of criteria objects with one object for each hop.
to.segments[n]RequiredObjectIn a node criteria object, you can include the following parameters: "relatedDesc", "query", "from", "as", "minDepth", "maxDepth", "options"
to.segments[n].relatedDescOptionalObjectAdd your relationship query as a valid simple query object.
to.segments[n].queryOptionalObjectFor this hop, enter a valid simple query to evaluate the to related items in related.relatedDesc.
to.segments[n].fromRequiredStringEnter the as name value from the previous segment. Only use this property for path building. For more information, see Building a path of hops between related items.
to.segments[n].asRequiredStringName the segment so that other segments can reference it.
to.segments[n].minDepthOptionalIntegerThe minimum number of hops you want to travel. The default value is 1. Only use this property for path discovery. For more information, see Discover paths between related items.
to.segments[n].maxDepthOptionalIntegerThe maximum number of hops you want to travel. The default value is 1. Only use this property for path discovery. For more information, see Discover paths between related items.
to.segments[n].optionsOptionalObjectAdd projection options for the segment response.
to.segments[n].options.projectOptionalObjectThe simple query projection you want in the response for the related items that match this segment query.
to.asRequiredStringName the property that contains the path query results in the response.
to.responseRequiredStringEnter “path”.
to.optionsOptionalObjectAdd page options for the path response.
to.options.pageOptionalObjectSet the response pagination options, such as _offset and _pageSize. The default value for _offset is 0 and for _pageSize is 25.

Request body options​

The $findWithRelatedGraph operator accepts an optional options object in the request body to control additional response behaviour.

OptionTypeDescription
includeRelsBooleanWhen set to true, the response includes a _referencedRels object containing metadata for each relationship traversed in the graph path. Defaults to false.
projectAllRefItemsBooleanWhen set to true, all properties are returned for every referenced item in _referencedItems. By default, only _id is returned. Ignored when an explicit project is defined on a segment in to.segments[n].options.project. Defaults to false.

Example request body:

{
"$findWithRelatedGraph": {
"start": { ... },
"to": { ... },
"options": {
"includeRels": true,
"projectAllRefItems": true
}
}
}

includeRels​

When includeRels is true, the response includes a _referencedRels object nested by source user item id, source user item version id, and source item id. Each entry in the innermost array describes one traversed relationship.

{
"_referencedRels": {
"<sourceUserItemId>": {
"<sourceUserItemVersionId>": {
"<sourceItemId>": [
{
"_id": "<relationship id>",
"_userType": "<relationship user type>",
"_isInverse": false,
"_customAttributes": {},
"_relatedUserItemId": "<user item id of the related node>",
"_relatedUserItemVersionId": "<user item version id of the related node>",
"_relatedToIds": ["<_id of the related item>"]
}
]
}
}
}
}

Table 2: _referencedRels entry fields

FieldTypeDescription
_idStringThe id of the Neo4j relationship.
_userTypeStringThe user-defined relationship type.
_isInverseBooleanfalse when the relationship is traversed in its natural direction (A → B). true when traversed in the reverse direction (B → A).
_customAttributesObjectAny additional properties stored on the relationship beyond _id and _userType.
_relatedUserItemIdStringThe user item id of the node at the other end of the relationship.
_relatedUserItemVersionIdStringThe user item version id of the node at the other end of the relationship.
_relatedToIdsArrayThe _id values of the items at the other end of this relationship, grouped across all path segments that share the same source node, relationship type, and direction.

projectAllRefItems​

By default, _referencedItems entries contain only the _id field of each referenced item. Set projectAllRefItems to true to retrieve all stored properties without specifying a project on every segment.

When both includeRels and projectAllRefItems are true, the response includes full item properties in _referencedItems and full relationship metadata in _referencedRels.