This creates "Customers also bought" recommendations functionality.This gives us the other products purchased by any customer who purchased the original product, and also products purchased by other customers who purchased those products. These types of "friend of a friend" queries are what graph databases excel at, while a traditional relational database is not ideal for this due to the arbitrary number of SQL joins which would be involved since this has performance implications and also typically requires programmatic SQL generation. The strength of each recommendation returned can be determined by its proximity to the starting product.

MongoDB Query The approach below is inspired by the way graph databases such as Neo4j or OrientDB store nodes (a.k.a. vertexes) and edges as separate objects in the database. For purposes of this example, we will create three collections in MongoDB: products, customers, and edges. The documents in the products collection will represent our e-commerce products and contain their attributes, and our customers will be stored as documents in the customers collection. The edge collection documents will represent the edges or relationships between nodes, including a relationship type, a from_id for the source node ID, a to_id for the target node ID, and any other custom properties you want to include. Note that one advantage of this approach is that we do not need to embed the edges within the nodes, in fact the nodes do not need to contain any relationship data whatsoever.