DEV Community

Cover image for Building an E-Commerce Support Chatbot: Part 2 - Building the Knowledge Base
Praveen
Praveen

Posted on

Building an E-Commerce Support Chatbot: Part 2 - Building the Knowledge Base

In this part, we’ll focus on setting up the database and preparing sample e-commerce order data that the chatbot will reference during conversations.

✅ What We'll Cover:

  • Setting up MongoDB as our database
  • Creating a schema for storing order information
  • Inserting sample order data
  • Writing functions to retrieve data for chatbot use

📦 1. Database Setup

We’ll use MongoDB. If you're running it locally, make sure MongoDB is installed and running. You can also use MongoDB Atlas (cloud).

Update your .env file with the MongoDB URI if you haven’t already:

MONGODB_URI=mongodb://localhost:27017/ecommerce
Enter fullscreen mode Exit fullscreen mode

📁 2. Schema Design

Let’s define a basic structure for order information:

{
  "orderId": "ORD123456",
  "customerName": "Jane Doe",
  "email": "jane@example.com",
  "items": [
    {
      "productName": "Wireless Mouse",
      "quantity": 1,
      "price": 25
    }
  ],
  "totalAmount": 25,
  "orderDate": "2024-03-01",
  "status": "Shipped"
}
Enter fullscreen mode Exit fullscreen mode

🛠 3. Inserting Sample Data

Create a script to insert sample data:

// backend/database/seed.js
const { MongoClient } = require('mongodb');
require('dotenv').config();

const sampleOrders = [
  {
    orderId: "ORD1001",
    customerName: "Alice Smith",
    email: "alice@example.com",
    items: [
      { productName: "Bluetooth Speaker", quantity: 1, price: 45 }
    ],
    totalAmount: 45,
    orderDate: new Date("2024-02-20"),
    status: "Delivered"
  },
  {
    orderId: "ORD1002",
    customerName: "Bob Lee",
    email: "bob@example.com",
    items: [
      { productName: "Laptop Stand", quantity: 2, price: 30 }
    ],
    totalAmount: 60,
    orderDate: new Date("2024-02-21"),
    status: "Shipped"
  }
];

(async () => {
  const client = new MongoClient(process.env.MONGODB_URI);
  try {
    await client.connect();
    const db = client.db('ecommerce');
    const orders = db.collection('orders');

    await orders.deleteMany({});
    await orders.insertMany(sampleOrders);
    console.log("Sample order data inserted.");
  } catch (err) {
    console.error("Data insertion failed", err);
  } finally {
    await client.close();
  }
})();
Enter fullscreen mode Exit fullscreen mode

Run the script:

node backend/database/seed.js
Enter fullscreen mode Exit fullscreen mode

🔍 4. Fetching Orders for the Chatbot

Now let's create a utility to fetch order details:

// backend/database/orders.js
const { connectToDatabase } = require('./connection');

async function getOrderById(orderId) {
  const db = await connectToDatabase();
  const order = await db.collection('orders').findOne({ orderId });
  return order;
}

module.exports = { getOrderById };
Enter fullscreen mode Exit fullscreen mode

This will later be used by the LangChain pipeline to fetch specific order info when a customer asks about their order.


✅ Next Steps (Part 3)

In the next part, we will:

  • Set up Pinecone for vector storage
  • Convert our order data into embeddings
  • Enable semantic search through LangChain

🚀 Stay tuned for Part 3!

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay