You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

NodeJS遍历字符串按数量批量保存至MongoDB并实现自增长度

Got it, let's work through this problem step by step. I'll walk you through a complete implementation that handles parsing your oddly formatted string, duplicates entries based on the Qty value, and sets up auto-incrementing fields for MongoDB.

Step 1: Setup Required Modules

First, we'll need the built-in querystring module (which you already started with) plus the MongoDB driver to interact with your database. If you haven't installed it yet, run npm install mongodb first.

Step 2: Parse the Input String

Your input string uses ? to separate individual package entries, and & (HTML-encoded &) to split key-value pairs. We'll first split the string into package chunks, clean up the encoding, then parse each chunk into a usable object.

Step 3: Generate Duplicated Entries

For each parsed package, we'll create as many copies as the Qty value specifies. Make sure to convert Qty and Price from strings to numbers since they're numeric values.

Step 4: Auto-Increment for MongoDB

MongoDB doesn't have a native auto-increment field like SQL, so we'll use a dedicated counters collection to track the next ID value. This is a standard pattern for auto-incrementing in MongoDB.

Complete Code Implementation

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

// Your original input string
const inputString = "Package=Package&Qty=1&Price=123?Package=Package Two&Qty=3&Price=702?Package=Package Three&Qty=1&Price=199?Package=Package One&Qty=4&Price=852?";

// MongoDB connection URI (update with your own DB details)
const mongoUri = 'mongodb://localhost:27017';
const dbName = 'your_database_name';
const collectionName = 'packages';
const counterCollectionName = 'counters';

// Function to get next auto-increment ID
async function getNextSequence(db, sequenceName) {
  const counter = await db.collection(counterCollectionName).findOneAndUpdate(
    { _id: sequenceName },
    { $inc: { sequence_value: 1 } },
    { upsert: true, returnDocument: 'after' }
  );
  return counter.value.sequence_value;
}

// Main processing function
async function processAndInsertPackages() {
  try {
    // 1. Parse the input string
    // Split into individual package entries, filter out empty strings from trailing ?
    const packageChunks = inputString.split('?').filter(chunk => chunk.trim() !== '');
    
    // Parse each chunk into an object
    const parsedPackages = packageChunks.map(chunk => {
      // Replace HTML-encoded & back to regular &
      const decodedChunk = chunk.replace(/&/g, '&');
      return querystring.parse(decodedChunk);
    });

    // 2. Generate duplicated entries based on Qty
    let finalEntries = [];
    for (const pkg of parsedPackages) {
      const qty = parseInt(pkg.Qty, 10);
      const price = parseFloat(pkg.Price);
      // Create qty number of entries
      for (let i = 0; i < qty; i++) {
        finalEntries.push({
          Package: pkg.Package,
          Price: price
        });
      }
    }

    // 3. Connect to MongoDB and insert entries
    const client = await MongoClient.connect(mongoUri);
    const db = client.db(dbName);

    // Add auto-increment IDs to each entry
    const entriesWithIds = await Promise.all(finalEntries.map(async entry => {
      const id = await getNextSequence(db, 'package_id');
      return {
        _id: id,
        ...entry
      };
    }));

    // Bulk insert all entries
    const result = await db.collection(collectionName).insertMany(entriesWithIds);
    console.log(`Successfully inserted ${result.insertedCount} entries`);

    client.close();
  } catch (error) {
    console.error('Error processing packages:', error);
  }
}

// Run the function
processAndInsertPackages();

Key Notes:

  • String Parsing: We use replace(/&amp;/g, '&') to fix the HTML-encoded ampersands so querystring.parse can correctly read the key-value pairs.
  • Auto-Increment: The getNextSequence function uses findOneAndUpdate with $inc to atomically increment the counter, which prevents race conditions if multiple processes are inserting data.
  • Data Types: We convert Qty to an integer and Price to a float to ensure proper numeric storage in MongoDB (instead of storing them as strings).
  • Bulk Insert: Using insertMany is more efficient than inserting one entry at a time, especially if you have large numbers of duplicates.

Just remember to update the mongoUri, dbName, collectionName values to match your actual MongoDB setup.

内容的提问来源于stack exchange,提问作者NewUser

火山引擎 最新活动