Graduate Program KB

To create the entities and relationships using Neo4j with JavaScript, follow these steps:

  1. Install the neo4j-driver package:
npm install neo4j-driver
  1. Initialize the Neo4j database connection:
const neo4j = require('neo4j-driver');

const uri = 'bolt://localhost:7687';
const user = 'neo4j';
const password = 'your_password';

const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
const session = driver.session();
  1. Define a function to create a user:
async function createUser(name, age) {
  const result = await session.run(
    'CREATE (user:User {name: $name, age: $age}) RETURN user',
    { name, age }
  );

  const createdUser = result.records[0].get('user');
  console.log(`Created user: ${createdUser.properties.name}`);
}
  1. Define a function to create a "FRIENDS_WITH" relationship between two users:
async function createFriendship(user1Name, user2Name) {
  const result = await session.run(
    `
      MATCH (user1:User {name: $user1Name})
      MATCH (user2:User {name: $user2Name})
      CREATE (user1)-[:FRIENDS_WITH]->(user2)
      RETURN user1, user2
    `,
    { user1Name, user2Name }
  );

  const user1 = result.records[0].get('user1');
  const user2 = result.records[0].get('user2');
  console.log(`Created friendship between ${user1.properties.name} and ${user2.properties.name}`);
}
  1. Load dummy data:
async function loadDummyData() {
  await createUser('Alice', 30);
  await createUser('Bob', 25);
  await createUser('Charlie', 22);

  await createFriendship('Alice', 'Bob');
  await createFriendship('Alice', 'Charlie');
  await createFriendship('Bob', 'Charlie');
}

loadDummyData();
  1. Define a function to query friends of a user:
async function getUserFriends(name) {
  const result = await session.run(
    `
      MATCH (user:User {name: $name})-[:FRIENDS_WITH]->(friends)
      RETURN friends
    `,
    { name }
  );

  console.log(`${name}'s friends:`);
  result.records.forEach((record) => {
    const friend = record.get('friends');
    console.log(friend.properties.name);
  });
}
  1. Query friends for a user:
async function main() {
  await loadDummyData();
  await getUserFriends('Alice');
  session.close();
  driver.close();
}

main();

This example demonstrates how to create entities and relationships in Neo4j using the neo4j-driver package and JavaScript. The database is initialized and loaded with dummy data, and you can query the friends of a user.

To expand on the examples, we will include additional features for both Neo4j and MongoDB. For Neo4j, we will add the ability to store and query posts created by users.

Expanded Neo4j Example:

  1. Define a function to create a post for a user:
async function createPost(authorName, content) {
  const result = await session.run(
    `
      MATCH (author:User {name: $authorName})
      CREATE (post:Post {content: $content, createdAt: datetime()})
      CREATE (author)-[:WROTE]->(post)
      RETURN post
    `,
    { authorName, content }
  );

  const createdPost = result.records[0].get('post');
  console.log(`Created post with ID ${createdPost.identity} by ${authorName}`);
}
  1. Load additional dummy data:
async function loadAdditionalDummyData() {
  await createPost('Alice', 'This is a post by Alice');
  await createPost('Bob', 'This is a post by Bob');
}
  1. Define a function to query posts written by a user:
async function getUserPosts(name) {
  const result = await session.run(
    `
      MATCH (author:User {name: $name})-[:WROTE]->(posts)
      RETURN posts
    `,
    { name }
  );

  console.log(`${name}'s posts:`);
  result.records.forEach((record) => {
    const post = record.get('posts');
    console.log(post.properties.content);
  });
}
  1. Query posts for a user:
async function main() {
  await loadDummyData();
  await loadAdditionalDummyData();
  await getUserFriends('Alice');
  await getUserPosts('Alice');
  session.close();
  driver.close();
}

main();

These expanded examples demonstrate how to create and query additional relationships and entities for Neo4j using JavaScript. We added the ability to create and query posts written by users.

Remember to call the loadAdditionalDummyData() function in the main() function to load the new data for the expanded examples.

To further expand on the examples, we will add the ability to store and query likes given by users to posts.

  1. Define a function to create a "LIKED" relationship between a user and a post:
async function likePost(userName, postId) {
  const result = await session.run(
    `
      MATCH (user:User {name: $userName})
      MATCH (post:Post) WHERE ID(post) = $postId
      CREATE (user)-[:LIKED]->(post)
      RETURN user, post
    `,
    { userName, postId: parseInt(postId) }
  );

  const user = result.records[0].get('user');
  const post = result.records[0].get('post');
  console.log(`${user.properties.name} liked post with ID ${post.identity}`);
}
  1. Load additional dummy data:
async function loadMoreDummyData() {
  const db = await getDb();
  const postsCollection = db.collection('posts');
  const post1 = await postsCollection.findOne({ title: 'A sample blog post' });
  const post2 = await postsCollection.findOne({ title: 'Another blog post' });

  await createComment(post1._id, 'Bob', 'This is a comment on the first post');
  await createComment(post2._id, 'Alice', 'This is a comment on the second post');
}
  1. Define a function to query the likes for a post:
async function getPostLikes(postId) {
  const result = await session.run(
    `
      MATCH (user:User)-[:LIKED]->(post:Post) WHERE ID(post) = $postId
      RETURN user
    `,
    { postId: parseInt(postId) }
  );

  console.log(`Likes for post with ID ${postId}:`);
  result.records.forEach((record) => {
    const user = record.get('user');
    console.log(user.properties.name);
  });
}
  1. Query likes for a post:
async function main() {
  await loadDummyData();
  await loadAdditionalDummyData();
  const alicePosts = await getUserPosts('Alice');
  const postId = alicePosts[0].identity.toString();
  await likePost('Bob', postId);
  await getPostLikes(postId);
  session.close();
  driver.close();
}

main();

These further expanded examples demonstrate how to create and query additional relationships and entities for Neo4j using JavaScript.

We added the ability to create and query likes given by users to posts.

Remember to call the loadMoreDummyData() function in the main() function to load the new data for the expanded examples.

These expanded examples highlight the power and flexibility of NoSQL databases in modeling various data structures and relationships, providing various querying options for different use cases.