0

I am very new at mongoDB, and I am trying to get the last Username(int) from dynamic json. The value of the username is always dynamic

Having mongoDB json as:

{
    "_id": "39ecb71e-9641-4c7a-9f97-ebe33458e125",
    "Username": "043020229",
    "Name": "test1",
    "Surname": "Tesfye",
    "Email": "ab@gmail.com",
    "Status": "UNCONF",
    "DateOfBirth": {
        "$date": "1982-02-25T15:24:17.923Z"
    },
    "RegistrationDate": {
        "$date": "2021-07-13T14:24:25.991Z"
    },
    "ActivitySouce": "test",
    "Password": "test"  
}

{
    "_id": "b6acb1ea-9629-42ea-94ce-71fc6812301d",
    "Username": "095262760",
    "Name": "test2",
    "Surname": "test",
    "Email": "ad@gmail.com",
    "Status": "ACT",
    "DateOfBirth": {
        "$date": "1981-08-31T14:24:54.166Z"
    },
    "RegistrationDate": {
        "$date": "2021-07-110T14:24:53.212Z"
    },
    "ActivitySouce": "test",
    "Password": "test"
}

What I tried using Jmeter JRS223 sampler is:

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
import static com.mongodb.client.model.Filters.eq;
import com.gmongo.*;
import com.mongodb.*;
import com.mongodb.DBCollection;                    // import DBCollection class
import org.bson.types.ObjectId
import com.mongodb.BasicDBObject
import com.mongodb.*


try {

    MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false");
    MongoDatabase database = mongoClient.getDatabase("FFF");
    MongoCollection<Document> collection = database.getCollection("Users");

    
    
    Document result = collection.find().sort(new Document("Username", -1)).limit(1);

    
    vars.put("Username", result.get("Username").toString());




    mongoClient.close()
}
catch (Exception e) {
    SampleResult.setSuccessful(false);
    SampleResult.setResponseCode("500");
    SampleResult.setResponseMessage("Exception: " + e);
}

the error that i am seeing is:

Response code:500
Response message:Exception: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.mongodb.client.internal.Java8FindIterableImpl@43dd792c' with class 'com.mongodb.client.internal.Java8FindIterableImpl' to class 'org.bson.Document'

How can I get the latest Username, having this set-up?

Every help is appreciated

vlatko606
  • 909
  • 1
  • 13
  • 21

1 Answers1

1

This line of code:

Document result = collection.find().sort(new Document("Username", -1)).limit(1);

gives you a FindIterable instance, you need to invoke the relevant function in order to get the Document

So you can quickly fix your script by adding .take(1) to the end of your line:

Document result = collection.find().sort(new Document("Username", -1)).limit(1).take(1);

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133