প্যাকেজ পরিচিতি
MongoDB-তে একটি সংগ্রহ MySQL-এর একটি টেবিলের সমতুল্য
মাইএসকিউএল টেবিল
- স্থির কাঠামো
- কলাম এবং সারি
- সংজ্ঞায়িত ডেটা প্রকার
- আগাম সংজ্ঞায়িত
MongoDB প্যাকেজ
- নমনীয় গঠন
- নথি (যেমন JSON)
- বিভিন্ন ধরনের ডেটা
- স্বয়ংক্রিয়ভাবে তৈরি হবে
একটি প্যাকেজ তৈরি করা হচ্ছে
MongoDB-তে একটি সংগ্রহ তৈরি করতে, createCollection() পদ্ধতি ব্যবহার করুন:
উদাহরণ
"গ্রাহক" নামে একটি সংগ্রহ তৈরি করুন:
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db("mydb");
dbo.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
উপরের কোডটি "demo_mongodb_createcollection.js" ফাইলে সংরক্ষণ করুন এবং ফাইলটি চালান:
C:\Users\Your Name>node demo_mongodb_createcollection.js
এটি আপনাকে এই ফলাফল দেবে:
Collection created!
গুরুত্বপূর্ণ নোট:
মঙ্গোডিবিতে, সামগ্রীটি উপলব্ধ না হওয়া পর্যন্ত একটি সংগ্রহ তৈরি করা হয় না!
মঙ্গোডিবি আসলে সংগ্রহ তৈরি করার আগে আপনার একটি নথি সন্নিবেশ করার জন্য অপেক্ষা করে।
আধুনিক সংশ্লেষণ
আধুনিক অ্যাসিঙ্ক/ওয়েট সিনট্যাক্স
const { MongoClient } = require('mongodb');
async function createCollectionModern() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Create collection explicitly
const collection = await database.createCollection("customers");
console.log("Collection created successfully:", collection.collectionName);
} catch (err) {
console.error("Error creating collection:", err);
} finally {
await client.close();
}
}
createCollectionModern();
স্বয়ংক্রিয় প্যাকেজ তৈরি
const { MongoClient } = require('mongodb');
async function autoCreateCollection() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Collection will be created automatically on first insert
const collection = database.collection("customers");
// Insert first document - this creates the collection
const result = await collection.insertOne({
name: "John Doe",
email: "john@example.com",
createdAt: new Date()
});
console.log("Collection created automatically with first document");
console.log("Document inserted with id:", result.insertedId);
} catch (err) {
console.error("Error:", err);
} finally {
await client.close();
}
}
autoCreateCollection();
প্যাকেজ বিকল্প
স্থির প্যাকেজ তৈরি
const { MongoClient } = require('mongodb');
async function createCappedCollection() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Create a capped collection (fixed size)
const collection = await database.createCollection("logs", {
capped: true,
size: 1000000, // 1MB in bytes
max: 1000 // Maximum 1000 documents
});
console.log("Capped collection created successfully");
} catch (err) {
console.error("Error creating capped collection:", err);
} finally {
await client.close();
}
}
createCappedCollection();
বৈধতা নিয়ম সহ প্যাকেজ
const { MongoClient } = require('mongodb');
async function createValidatedCollection() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Create collection with validation rules
const collection = await database.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "email"],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType: "string",
pattern: "^.+@.+\\..+$",
description: "must be a valid email and is required"
},
age: {
bsonType: "int",
minimum: 0,
maximum: 150,
description: "must be an integer between 0 and 150"
}
}
}
}
});
console.log("Validated collection created successfully");
} catch (err) {
console.error("Error creating validated collection:", err);
} finally {
await client.close();
}
}
createValidatedCollection();
প্যাকেজ ব্যবস্থাপনা
তালিকা প্যাকেজ
const { MongoClient } = require('mongodb');
async function listCollections() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
const collections = await database.listCollections().toArray();
console.log("Collections in database:");
collections.forEach(collection => {
console.log(` - ${collection.name}`);
});
} catch (err) {
console.error("Error listing collections:", err);
} finally {
await client.close();
}
}
listCollections();
প্যাকেজ উপলব্ধতা পরীক্ষা করুন
const { MongoClient } = require('mongodb');
async function checkCollectionExists() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
const collections = await database.listCollections().toArray();
const exists = collections.some(col => col.name === "customers");
if (exists) {
console.log("Collection 'customers' exists");
} else {
console.log("Collection 'customers' does not exist");
}
} catch (err) {
console.error("Error checking collection:", err);
} finally {
await client.close();
}
}
checkCollectionExists();
প্যাকেজ প্রকার এবং ব্যবহার ক্ষেত্রে
| প্যাকেজ প্রকার | ব্যাখ্যা | কেস ব্যবহার করুন |
|---|---|---|
| সাধারণ সেট | কোন সীমাবদ্ধতা ছাড়া একটি নমনীয় প্যাকেজ | ব্যবহারকারীর ডেটা, পণ্য, অফিসিয়াল ডেটা |
| স্থির প্যাকেজ | নির্দিষ্ট আকারের একটি সেট | লগ, প্লট ডেটা, সেন্সর রিডিং |
| যাচাইকৃত প্যাকেজ | ডেটা যাচাইকরণের নিয়মগুলির একটি সেট | সংবেদনশীল ডেটা, ব্যবহারকারীর ইনপুট, API ডেটা |
| একটি অনির্দিষ্ট কালেকশন | একটি প্যাকেজ যা অনির্দিষ্টকালের জন্য ডেটা সঞ্চয় করে | ঐতিহাসিক তথ্য, সংরক্ষণাগার |
প্যাকেজ তৈরির জন্য সর্বোত্তম অনুশীলন
নামকরণের রীতি
- বহুবচন বিশেষ্য ব্যবহার করুন (ব্যবহারকারী, পণ্য)
- ছোট হাতের অক্ষর ব্যবহার করুন
- আন্ডারস্কোর এড়িয়ে চলুন
- বর্ণনামূলক নাম ব্যবহার করুন
কাঠামোগত কৌশল
- প্রয়োজন অনুযায়ী বিভিন্ন প্যাকেজ তৈরি করুন
- ডেটা অ্যাক্সেস প্যাটার্ন বিবেচনা করুন
- স্কেলিং প্রয়োজনীয়তা জন্য পরিকল্পনা
- পরিকল্পনা কোডিং কৌশল
ডেটা অখণ্ডতা
- সংবেদনশীল তথ্যের জন্য বৈধতা নিয়ম ব্যবহার করুন
- প্রয়োজনীয় ক্ষেত্র নির্দিষ্ট করুন
- ডেটা টাইপ সীমাবদ্ধতা সেট করুন
- প্যাকেজ অনুমতি কনফিগার করুন
সম্পূর্ণ উদাহরণ
প্যাকেজ তৈরি এবং পরিচালনার একটি সম্পূর্ণ উদাহরণ:
const { MongoClient } = require('mongodb');
class CollectionManager {
constructor(connectionString, dbName) {
this.connectionString = connectionString;
this.dbName = dbName;
this.client = new MongoClient(connectionString);
}
async connect() {
try {
await this.client.connect();
console.log("Connected to MongoDB server");
this.database = this.client.db(this.dbName);
return true;
} catch (error) {
console.error("Connection failed:", error);
return false;
}
}
async createCollection(collectionName, options = {}) {
try {
const collection = await this.database.createCollection(collectionName, options);
console.log(`Collection '${collectionName}' created successfully`);
return collection;
} catch (error) {
console.error(`Error creating collection '${collectionName}':`, error);
throw error;
}
}
async getOrCreateCollection(collectionName) {
try {
// Check if collection exists
const collections = await this.database.listCollections().toArray();
const exists = collections.some(col => col.name === collectionName);
if (exists) {
console.log(`Collection '${collectionName}' already exists`);
return this.database.collection(collectionName);
} else {
return await this.createCollection(collectionName);
}
} catch (error) {
console.error(`Error getting/creating collection '${collectionName}':`, error);
throw error;
}
}
async listAllCollections() {
try {
const collections = await this.database.listCollections().toArray();
console.log(`Found ${collections.length} collections:`);
collections.forEach(collection => {
console.log(` - ${collection.name}`);
});
return collections;
} catch (error) {
console.error("Error listing collections:", error);
throw error;
}
}
async close() {
try {
await this.client.close();
console.log("Connection closed");
} catch (error) {
console.error("Error closing connection:", error);
}
}
}
// Usage example
async function main() {
const connectionString = "mongodb://localhost:27017/";
const dbName = "mydb";
const collectionManager = new CollectionManager(connectionString, dbName);
try {
const connected = await collectionManager.connect();
if (!connected) return;
// Create a regular collection
await collectionManager.createCollection("users");
// Create a collection with validation
await collectionManager.createCollection("products", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "price"],
properties: {
name: { bsonType: "string" },
price: { bsonType: "double", minimum: 0 }
}
}
}
});
// Get or create collection (idempotent)
const customersCollection = await collectionManager.getOrCreateCollection("customers");
// List all collections
await collectionManager.listAllCollections();
console.log("Collection setup completed successfully!");
} catch (error) {
console.error("Main function error:", error);
} finally {
await collectionManager.close();
}
}
// Run the example
main();