JMeter中Groovy脚本执行MongoDB插入操作失败问题排查
Hey there, let’s work through this insert problem step by step! You’ve got the connection working, which is half the battle—now let’s get those documents into MongoDB properly.
Common Pitfalls & Fixes
1. Correctly Handle MongoDB-Specific Data Types
The document you shared uses ISODate and NumberLong—these can’t be written as raw strings in Groovy; you need to map them to MongoDB-compatible types. Here’s how to construct your document properly:
import com.mongodb.client.MongoClients import org.bson.Document import java.time.Instant // Reuse your existing MongoClient (avoid creating a new one per sampler!) def mongoClient = MongoClients.create("mongodb://your-host:27017") def db = mongoClient.getDatabase("your-database-name") def collection = db.getCollection("your-collection-name") // Build the nested cart document with valid types def cartDocument = new Document() .append("schema", "http://dell.com/dcp/schemas/cart/3.0.0#") .append("_id", "s5ChQonvAUGKM6s2Yq8Z31") .append("createdOn", new Document() .append("DateTime", Date.from(Instant.parse("2018-03-07T06:54:01.242Z"))) .append("Ticks", new NumberLong(636560222412422269)) .append("Offset", 330)) .append("lastModifiedOn", new Document() .append("DateTime", Date.from(Instant.parse("2018-03-07T06:54:01.245Z"))) .append("Ticks", new NumberLong(636560222412422272)) // Fill in your missing value here .append("Offset", 330)) // Wrap it in the root "cart" field def finalDocument = new Document("cart", cartDocument)
2. Add Error Handling to Debug
You won’t know exactly what’s breaking unless you log exceptions. Wrap your insert logic in a try-catch block to get detailed error messages:
try { collection.insertOne(finalDocument) log.info("✅ Document inserted successfully!") } catch (Exception e) { log.error("❌ Insert failed: " + e.getMessage(), e) throw e // Marks the sampler as failed in JMeter }
Check JMeter’s jmeter.log file (or the Log Viewer in the GUI) for the full stack trace—this will tell you if it’s a duplicate _id, permission issue, or type mismatch.
3. Verify Permissions & Collection Existence
- Make sure the MongoDB user you’re connecting with has write permissions on the target database/collection.
- Double-check that the collection name is spelled correctly (MongoDB is case-sensitive!).
4. Avoid Duplicate _id Values
If you’re reusing the same _id value (s5ChQonvAUGKM6s2Yq8Z31) across multiple sampler runs, MongoDB will reject subsequent inserts because _id must be unique. Either:
- Generate a unique
_ideach time (e.g.,new ObjectId()instead of a hardcoded string), or - Use
replaceOnewithupsert: trueif you want to overwrite existing documents with the same_id.
5. Check Driver Compatibility
Ensure the MongoDB Java driver version you’re using in JMeter matches your MongoDB server version. For example:
- MongoDB 5.x → Driver 4.x
- MongoDB 6.x → Driver 4.7+
You can add the driver JARs to JMeter’s lib folder (or use the Maven Dependency Plugin if you’re using a build system).
内容的提问来源于stack exchange,提问作者sam

