Skip to main content

Bulk Relationships

Connect or disconnect many records at once by matching on property keys. Useful after importing flat data from CSV, MongoDB exports, or external APIs where related records arrive separately with reference fields.


Bulk Create by Key Match

Connect records by matching a property on the source label to a property on the target label. All pairs where the values are equal are linked.

db.relationships.create_many()

# MOVIE.directorId == DIRECTOR.id → create DIRECTED_BY
db.relationships.create_many(
source={"label": "MOVIE", "key": "directorId"},
target={"label": "DIRECTOR", "key": "id"},
type="DIRECTED_BY",
direction="out"
)
# Creates: (MOVIE) -[:DIRECTED_BY]-> (DIRECTOR) where MOVIE.directorId = DIRECTOR.id

Parameters

FieldTypeDescription
source{label, key?, where?}Source selector — label + key for equality join, or where for filter-based match
target{label, key?, where?}Target selector — same
typestringRelationship type to create
direction"in" | "out"Direction from source to target
manyToManybooleanCartesian product mode (see below)

Bulk Delete by Key Match

Same shape as createMany. Removes relationships where the key values match.

db.relationships.delete_many(
source={"label": "MOVIE", "key": "directorId"},
target={"label": "DIRECTOR", "key": "id"},
type="DIRECTED_BY",
direction="out"
)

Many-to-Many (Cartesian)

Connect or disconnect all records matching source conditions with all records matching target conditions.

warning

This mode creates a cartesian product — all source × all target pairs are linked. It is opt-in and requires non-empty where filters on both sides to prevent accidental unbounded operations.

# Tag all sci-fi movies with all genre tags
db.relationships.create_many(
source={"label": "MOVIE", "where": {"genre": "sci-fi"}},
target={"label": "TAG", "where": {"category": "genre"}},
type="HAS_TAG",
many_to_many=True
)

Delete example:

# Remove HAS_TAG between all sci-fi movies and all genre tags
db.relationships.delete_many(
source={"label": "MOVIE", "where": {"genre": "sci-fi"}},
target={"label": "TAG", "where": {"category": "genre"}},
type="HAS_TAG",
many_to_many=True
)
Both source.where and target.where must be non-empty when manyToMany: true.

See also