Node.js MongoDB Insert

Node.js ஐப் பயன்படுத்தி MongoDB தொகுப்புகளில் ஆவணங்களைச் செருக கற்றுக்கொள்ளுங்கள்

தொகுப்பில் செருகுதல்

ஒரு பதிவை, அல்லது MongoDB இல் ஆவணம் என்று அழைக்கப்படுவதை, ஒரு தொகுப்பில் செருக, நாங்கள் insertOne() முறையைப் பயன்படுத்துகிறோம்.

MongoDB இல் உள்ள ஒரு ஆவணம் MySQL இல் உள்ள பதிவுக்கு சமமானதாகும்

insertOne() முறையின் முதல் அளவுரு, நீங்கள் செருக விரும்பும் ஆவணத்தில் உள்ள ஒவ்வொரு புலத்தின் பெயர்(கள்) மற்றும் மதிப்பு(கள்) ஆகியவற்றைக் கொண்ட ஒரு பொருளாகும்.

இது ஒரு கால்பேக் செயல்பாட்டையும் எடுக்கும், அங்கு நீங்கள் எந்தப் பிழைகளுடனும் அல்லது செருகுதலின் முடிவுடனும் பணியாற்றலாம்:

எடுத்துக்காட்டு

"customers" தொகுப்பில் ஒரு ஆவணத்தைச் செருகவும்:

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");
  let myobj = { name: "Company Inc", address: "Highway 37" };
  dbo.collection("customers").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
    db.close();
  });
});

மேலே உள்ள குறியீட்டை "demo_mongodb_insert.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:

C:\Users\Your Name>node demo_mongodb_insert.js

இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:

1 document inserted

💡 குறிப்பு:

இல்லாத தொகுப்பில் ஆவணங்களைச் செருக முயற்சித்தால், MongoDB தானாகவே தொகுப்பை உருவாக்கும்.

பல ஆவணங்களைச் செருகுதல்

MongoDB இல் ஒரு தொகுப்பில் பல ஆவணங்களைச் செருக, நாங்கள் insertMany() முறையைப் பயன்படுத்துகிறோம்.

insertMany() முறையின் முதல் அளவுரு, நீங்கள் செருக விரும்பும் தரவைக் கொண்ட பொருட்களின் வரிசையாகும்.

இது ஒரு கால்பேக் செயல்பாட்டையும் எடுக்கும், அங்கு நீங்கள் எந்தப் பிழைகளுடனும் அல்லது செருகுதலின் முடிவுடனும் பணியாற்றலாம்:

எடுத்துக்காட்டு

"customers" தொகுப்பில் பல ஆவணங்களைச் செருகவும்:

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");
  let myobj = [
    { name: 'John', address: 'Highway 71'},
    { name: 'Peter', address: 'Lowstreet 4'},
    { name: 'Amy', address: 'Apple st 652'},
    { name: 'Hannah', address: 'Mountain 21'},
    { name: 'Michael', address: 'Valley 345'},
    { name: 'Sandy', address: 'Ocean blvd 2'},
    { name: 'Betty', address: 'Green Grass 1'},
    { name: 'Richard', address: 'Sky st 331'},
    { name: 'Susan', address: 'One way 98'},
    { name: 'Vicky', address: 'Yellow Garden 2'},
    { name: 'Ben', address: 'Park Lane 38'},
    { name: 'William', address: 'Central st 954'},
    { name: 'Chuck', address: 'Main Road 989'},
    { name: 'Viola', address: 'Sideway 1633'}
  ];
  dbo.collection("customers").insertMany(myobj, function(err, res) {
    if (err) throw err;
    console.log("Number of documents inserted: " + res.insertedCount);
    db.close();
  });
});

மேலே உள்ள குறியீட்டை "demo_mongodb_insert_multiple.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:

C:\Users\Your Name>node demo_mongodb_insert_multiple.js

இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:

Number of documents inserted: 14

முடிவு பொருள்

insertMany() முறையை இயக்கும் போது, ஒரு முடிவு பொருள் திருப்பி அனுப்பப்படும்.

முடிவு பொருள் செருகுதல் தரவுத்தளத்தை எவ்வாறு பாதித்தது என்பதைப் பற்றிய தகவல்களைக் கொண்டுள்ளது.

மேலே உள்ள எடுத்துக்காட்டிலிருந்து திருப்பி அனுப்பப்பட்ட பொருள் இப்படித் தெரிகிறது:

{
  result: { ok: 1, n: 14 },
  ops: [
    { name: 'John', address: 'Highway 71', _id: 58fdbf5c0ef8a50b4cdd9a84 },
    { name: 'Peter', address: 'Lowstreet 4', _id: 58fdbf5c0ef8a50b4cdd9a85 },
    { name: 'Amy', address: 'Apple st 652', _id: 58fdbf5c0ef8a50b4cdd9a86 },
    { name: 'Hannah', address: 'Mountain 21', _id: 58fdbf5c0ef8a50b4cdd9a87 },
    { name: 'Michael', address: 'Valley 345', _id: 58fdbf5c0ef8a50b4cdd9a88 },
    { name: 'Sandy', address: 'Ocean blvd 2', _id: 58fdbf5c0ef8a50b4cdd9a89 },
    { name: 'Betty', address: 'Green Grass 1', _id: 58fdbf5c0ef8a50b4cdd9a8a },
    { name: 'Richard', address: 'Sky st 331', _id: 58fdbf5c0ef8a50b4cdd9a8b },
    { name: 'Susan', address: 'One way 98', _id: 58fdbf5c0ef8a50b4cdd9a8c },
    { name: 'Vicky', address: 'Yellow Garden 2', _id: 58fdbf5c0ef8a50b4cdd9a8d },
    { name: 'Ben', address: 'Park Lane 38', _id: 58fdbf5c0ef8a50b4cdd9a8e },
    { name: 'William', address: 'Central st 954', _id: 58fdbf5c0ef8a50b4cdd9a8f },
    { name: 'Chuck', address: 'Main Road 989', _id: 58fdbf5c0ef8a50b4cdd9a90 },
    { name: 'Viola', address: 'Sideway 1633', _id: 58fdbf5c0ef8a50b4cdd9a91 } ],
  insertedCount: 14,
  insertedIds: [
    58fdbf5c0ef8a50b4cdd9a84,
    58fdbf5c0ef8a50b4cdd9a85,
    58fdbf5c0ef8a50b4cdd9a86,
    58fdbf5c0ef8a50b4cdd9a87,
    58fdbf5c0ef8a50b4cdd9a88,
    58fdbf5c0ef8a50b4cdd9a89,
    58fdbf5c0ef8a50b4cdd9a8a,
    58fdbf5c0ef8a50b4cdd9a8b,
    58fdbf5c0ef8a50b4cdd9a8c,
    58fdbf5c0ef8a50b4cdd9a8d,
    58fdbf5c0ef8a50b4cdd9a8e,
    58fdbf5c0ef8a50b4cdd9a8f,
    58fdbf5c0ef8a50b4cdd9a90,
    58fdbf5c0ef8a50b4cdd9a91 ]
}

பண்புகளின் மதிப்புகளை இப்படி காட்டலாம்:

எடுத்துக்காட்டு

செருகப்பட்ட ஆவணங்களின் எண்ணிக்கையைத் திருப்பி அனுப்பவும்:

console.log(res.insertedCount)

இது இந்த முடிவை உருவாக்கும்:

14

_id புலம்

நீங்கள் _id புலத்தைக் குறிப்பிடவில்லை என்றால், MongoDB உங்களுக்காக ஒன்றைச் சேர்த்து ஒவ்வொரு ஆவணத்திற்கும் ஒரு தனிப்பட்ட id ஐ ஒதுக்கும்.

மேலே உள்ள எடுத்துக்காட்டில் _id புலம் குறிப்பிடப்படவில்லை, மேலும் முடிவு பொருளிலிருந்து நீங்கள் பார்க்க முடியும் என, MongoDB ஒவ்வொரு ஆவணத்திற்கும் ஒரு தனிப்பட்ட _id ஐ ஒதுக்கியது.

நீங்கள் _id புலத்தைக் குறிப்பிட்டால், மதிப்பு ஒவ்வொரு ஆவணத்திற்கும் தனிப்பட்டதாக இருக்க வேண்டும்:

எடுத்துக்காட்டு

"products" அட்டவணையில் குறிப்பிடப்பட்ட _id புலங்களுடன் மூன்று பதிவுகளைச் செருகவும்:

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");
  let myobj = [
    { _id: 154, name: 'Chocolate Heaven'},
    { _id: 155, name: 'Tasty Lemon'},
    { _id: 156, name: 'Vanilla Dream'}
  ];
  dbo.collection("products").insertMany(myobj, function(err, res) {
    if (err) throw err;
    console.log(res);
    db.close();
  });
});

மேலே உள்ள குறியீட்டை "demo_mongodb_insert_id.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:

C:\Users\Your Name>node demo_mongodb_insert_id.js

இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:

{
  result: { ok: 1, n: 3 },
  ops: [
    { _id: 154, name: 'Chocolate Heaven },
    { _id: 155, name: 'Tasty Lemon },
    { _id: 156, name: 'Vanilla Dream } ],
  insertedCount: 3,
  insertedIds: [
    154,
    155,
    156 ]
}

நவீன செருகல் எடுத்துக்காட்டுகள்

நவீன async/await தொடரியல்

const { MongoClient } = require('mongodb');

async function insertSingleDocument() {
  const uri = "mongodb://localhost:27017/";
  const client = new MongoClient(uri);

  try {
    await client.connect();
    const database = client.db("mydb");
    const collection = database.collection("customers");
    
    const result = await collection.insertOne({
      name: "Modern Company",
      address: "Digital Street 123",
      email: "info@moderncompany.com",
      createdAt: new Date()
    });
    
    console.log(`Document inserted with _id: ${result.insertedId}`);
    
  } catch (err) {
    console.error("Error inserting document:", err);
  } finally {
    await client.close();
  }
}

insertSingleDocument();

பல ஆவணங்களை நவீன முறையில் செருகுதல்

const { MongoClient } = require('mongodb');

async function insertMultipleDocuments() {
  const uri = "mongodb://localhost:27017/";
  const client = new MongoClient(uri);

  try {
    await client.connect();
    const database = client.db("mydb");
    const collection = database.collection("users");
    
    const users = [
      { name: "Alice", age: 25, city: "New York" },
      { name: "Bob", age: 30, city: "London" },
      { name: "Charlie", age: 35, city: "Tokyo" }
    ];
    
    const result = await collection.insertMany(users);
    
    console.log(`${result.insertedCount} documents were inserted`);
    console.log("Inserted IDs:", result.insertedIds);
    
  } catch (err) {
    console.error("Error inserting documents:", err);
  } finally {
    await client.close();
  }
}

insertMultipleDocuments();

செருகல் சிறந்த நடைமுறைகள்

செயல்திறன்

  • பெரிய செருகுதல்களுக்கு insertMany() பயன்படுத்தவும்
  • தொகுதி செருகல்களைப் பயன்படுத்தவும்
  • தேவையற்ற புலங்களைத் தவிர்க்கவும்
  • சரியான குறியீட்டு உத்திகளைப் பயன்படுத்தவும்

பாதுகாப்பு

  • பயனர் உள்ளீட்டை எப்போதும் சரிபார்க்கவும்
  • தரவு சுத்திகரிப்பு செயல்பாடுகளைப் பயன்படுத்தவும்
  • சரிபார்க்கப்பட்ட தொகுப்புகளைப் பயன்படுத்தவும்
  • பிழை கையாளுதலையும் கண்காணிப்பையும் பயன்படுத்தவும்

தரவு வடிவமைப்பு

  • நிலையான புலப் பெயர்களைப் பயன்படுத்தவும்
  • தேதி/நேரத்தை சரியாக வடிவமைக்கவும்
  • தேவையான புலங்களைக் குறிப்பிடவும்
  • ஆவண அளவைக் கண்காணிக்கவும்

முழுமையான எடுத்துக்காட்டு

அனைத்து செருகல் நுட்பங்களையும் உள்ளடக்கிய முழுமையான எடுத்துக்காட்டு:

const { MongoClient } = require('mongodb');

class DataInserter {
  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 insertSingle(collectionName, document) {
    try {
      const collection = this.database.collection(collectionName);
      const result = await collection.insertOne(document);
      console.log(`Inserted document with _id: ${result.insertedId}`);
      return result;
    } catch (error) {
      console.error("Error inserting single document:", error);
      throw error;
    }
  }

  async insertMultiple(collectionName, documents) {
    try {
      const collection = this.database.collection(collectionName);
      const result = await collection.insertMany(documents);
      console.log(`Inserted ${result.insertedCount} documents`);
      return result;
    } catch (error) {
      console.error("Error inserting multiple documents:", error);
      throw error;
    }
  }

  async insertWithCustomIds(collectionName, documentsWithIds) {
    try {
      const collection = this.database.collection(collectionName);
      const result = await collection.insertMany(documentsWithIds);
      console.log(`Inserted ${result.insertedCount} documents with custom IDs`);
      return result;
    } catch (error) {
      console.error("Error inserting documents with custom IDs:", 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 inserter = new DataInserter(connectionString, dbName);

  try {
    const connected = await inserter.connect();
    if (!connected) return;

    // Insert single document
    await inserter.insertSingle("customers", {
      name: "Tech Solutions Inc",
      address: "Tech Park 456",
      industry: "Technology",
      founded: 2020
    });

    // Insert multiple documents
    const employees = [
      { name: "Sarah", position: "Developer", salary: 60000 },
      { name: "Mike", position: "Designer", salary: 55000 },
      { name: "Lisa", position: "Manager", salary: 75000 }
    ];
    await inserter.insertMultiple("employees", employees);

    // Insert with custom IDs
    const products = [
      { _id: "prod001", name: "Laptop", price: 999.99, category: "Electronics" },
      { _id: "prod002", name: "Mouse", price: 29.99, category: "Electronics" },
      { _id: "prod003", name: "Keyboard", price: 79.99, category: "Electronics" }
    ];
    await inserter.insertWithCustomIds("products", products);

    console.log("All insert operations completed successfully!");
    
  } catch (error) {
    console.error("Main function error:", error);
  } finally {
    await inserter.close();
  }
}

// Run the example
main();

பயிற்சி

MongoDB இல் உள்ள ஒரு ஆவணம் MySQL இல் உள்ள ______ க்கு சமமானதாகும்.

தரவுத்தளம்
✗ தவறு! தரவுத்தளம் என்பது MongoDB இல் உள்ள தரவுத்தளத்திற்கு சமமானதாகும், தனிப்பட்ட ஆவணம் அல்ல
அட்டவணை
✗ தவறு! அட்டவணை என்பது MongoDB இல் உள்ள தொகுப்புக்கு சமமானதாகும், தனிப்பட்ட ஆவணம் அல்ல
பதிவு
✓ சரி! MongoDB இல் உள்ள ஒரு ஆவணம் MySQL இல் உள்ள பதிவுக்கு சமமானதாகும். இரண்டும் தனிப்பட்ட தரவு உருப்படிகளைக் குறிக்கின்றன
நெடுவரிசை
✗ தவறு! நெடுவரிசை என்பது MySQL இல் உள்ள ஒரு தனி தரவு புலமாகும், முழு பதிவு அல்ல