Meteor.js批量校验导入数据的reference字段是否已存在于集合
Hey there, I've got you covered on this batch reference check for your Meteor app. Here's a clean, efficient way to handle verifying duplicates from your imported XLSX-to-JSON data:
First, pull all the reference values out of your converted JSON data into a dedicated array—this makes it easy to run a single batch query later.
// Assume your imported JSON data is stored here const importedJsonData = [ { reference: '102020026', name: 'Sample Item 1' }, { reference: '102020027', name: 'Sample Item 2' }, // ... rest of your imported entries ]; // Extract all reference values into an array const incomingReferences = importedJsonData.map(item => item.reference);
Instead of looping through each reference and running individual find() calls (which is inefficient), use Meteor's $in operator to check all values in one go. This cuts down on database roundtrips drastically, especially with your existing 100+ collection documents.
// Replace `YourCollection` with your actual collection name const existingMatchingReferences = YourCollection.find( { reference: { $in: incomingReferences } }, // Match any reference in our incoming array { fields: { reference: 1, _id: 0 } } // Only fetch the reference field to reduce data transfer ).fetch().map(doc => doc.reference);
Compare the incoming references array with the existing matches to spot duplicates, then throw an error if any are found.
// Filter out references that already exist in the collection const duplicateReferences = incomingReferences.filter(ref => existingMatchingReferences.includes(ref)); // Throw error if duplicates are detected if (duplicateReferences.length > 0) { throw new Error(`Duplicate reference values found: ${duplicateReferences.join(', ')}`); }
If you're dealing with a lot of imported data, offloading the check to a server-side Meteor method is smarter. It reduces client-side load and leverages the server's faster database access.
Server-Side Code (add to your server directory):
// First, add the check package if you haven't: meteor add check import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; import { YourCollection } from '../collections/your-collection.js'; // Adjust path as needed Meteor.methods({ checkDuplicateReferences(references) { check(references, [String]); // Validate input to ensure it's an array of strings const existingRefs = YourCollection.find( { reference: { $in: references } }, { fields: { reference: 1 } } ).fetch().map(doc => doc.reference); return existingRefs; } });
Client-Side Call:
Meteor.call('checkDuplicateReferences', incomingReferences, (error, existingMatches) => { if (error) { console.error('Error checking duplicates:', error); return; } const duplicates = incomingReferences.filter(ref => existingMatches.includes(ref)); if (duplicates.length) { alert(`Import failed: Duplicate references detected - ${duplicates.join(', ')}`); // Add your custom error handling here (e.g., stop the import process) } else { console.log('All references are unique! Proceed with import.'); // Continue with your import logic } });
Key note: Make sure the reference field in your collection is stored as the same data type as the values from your JSON (e.g., strings) to avoid mismatches.
内容的提问来源于stack exchange,提问作者Pedro Martins

