MongoDB – Create Collection
Now that the database is created, let’s define some collections for the insurance company. For an insurance company, you might want to create collections such as:
- Customers: To store customer information.
- Policies: To store insurance policies issued to customers.
- Claims: To store claim information for policies.
- Payments: To store payments made for premiums.
4. Example of Creating Collections and Documents
As we have created insuranceDB, so we will use that
use insuranceDB
a. Create customers Collection
Let’s insert a sample document into the customers collection, which will automatically create the collection if it doesn’t exist:
db.customers.insertOne({
_id: 1,
firstName: "John",
lastName: "Doe",
dateOfBirth: new Date("1985-04-15"),
contactInfo: {
phone: "123-456-7890",
email: "john.doe@example.com",
address: "123 Elm Street, Springfield"
},
policies: []
})
This document stores basic customer information, including contact information and an empty array for policies (we’ll reference policies later).
b. Create policies Collection
Now, insert a document into the policies collection:
db.policies.insertOne({
_id: 101,
policyNumber: "INS-2023-001",
policyType: "Health Insurance",
startDate: new Date("2023-01-01"),
endDate: new Date("2023-12-31"),
premiumAmount: 1200,
coverageAmount: 50000,
customerId: 1 // Reference to the customer document
})
In this example, the policy document includes information about the policy type, premium, coverage amount, and a reference to the customerId.
c. Create claims Collection
Now, let’s add a sample claim document:
db.claims.insertOne({
_id: 201,
claimNumber: "CLAIM-2023-001",
policyNumber: "INS-2023-001",
claimAmount: 2000,
claimDate: new Date("2023-06-15"),
status: "Pending",
customerId: 1
})
The claims document includes information about the claim amount, status, claim date, and a reference to the customerId and policyNumber.
d. Create payments Collection
Next, let’s insert a document into the payments collection:
db.payments.insertOne({
_id: 301,
paymentDate: new Date("2023-02-01"),
paymentAmount: 1200,
paymentMethod: "Credit Card",
policyNumber: "INS-2023-001",
customerId: 1
})
The payments document includes details about the payment amount, payment method, and a reference to the policyNumber and customerId.
Recent Comments