gpt4 book ai didi

mongodb - 删除操作无法在 Jmeter 中使用 groovy 删除 mongo 中的文档

转载 作者:行者123 更新时间:2023-12-04 10:47:40 34 4
gpt4 key购买 nike

IAm 尝试使用以下代码删除我通过我的脚本使用 deleteone 和 deletemany 创建的文档:

import com.mongodb.client.MongoCollection;
import org.bson.Document;
import static com.mongodb.client.model.Filters.*;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.util.Arrays;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;



try {

String mongoUser = vars.get("mongouser");
String userDB = vars.get("userdb");
char[] password = vars.get("password").toCharArray();
MongoCredential credential = MongoCredential.createCredential(mongoUser, userDB, password);
MongoClientSettings settings = MongoClientSettings.builder()
.applyToClusterSettings {builder ->
builder.hosts(Collections.singletonList(new ServerAddress(vars.get("mongohost"), vars.get("mongoPort") as int)))}
.credential(credential)
.build();
MongoClient mongoClient = MongoClients.create(settings);


MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
MongoCollection<Document> collection = database.getCollection(vars.get("collectionName1"));
vars.putObject("collection1", collection);

collection.deleteone(eq("EmployeeName", "Test Automation through Jmeter"));

return "Document deleted";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);}

收到此错误:
Response message: Exception: groovy.lang.MissingMethodException: No signature of method: com.mongodb.client.internal.MongoCollectionImpl.deleteone() is applicable for argument types: (com.mongodb.client.model.Filters$ 

SimpleEncodingFilter) 值:[Filter{fieldName='EmployeeName', value=Test Automation through Jmeter}]
可能的解决方案:deleteOne(org.bson.conversions.Bson), deleteOne(com.mongodb.client.ClientSession, org.bson.conversions.Bson), deleteOne(org.bson.conversions.Bson, com.mongodb.client.model .DeleteOptions), deleteOne(com.mongodb.client.ClientSession, org.bson.conversions.Bson, com.mongodb.client.model.DeleteOptions), deleteMany(org.bson.conversions.Bson), deleteMany(com.mongodb. client.ClientSession, org.bson.conversions.Bson)

我究竟做错了什么?

最佳答案

我相信这个方法叫做 deleteOne ,不是 deleteone ,所以正确的调用方式应该是:

collection.deleteOne(eq("EmployeeName", "Test Automation through Jmeter"));

另请参阅 MongoCollection 的 javadocs : https://mongodb.github.io/mongo-java-driver/3.6/javadoc/?com/mongodb/client/MongoCollection.html

关于mongodb - 删除操作无法在 Jmeter 中使用 groovy 删除 mongo 中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59627829/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com