0
votes

I have collection called 'jobs' with a document :

{
"_id": "4bb131682d0682845098eabe",
"jobId": "1403610",
"refmdList": [
    {
        "_id": "4bb11af1f8d48284e0b2ef87",
        "addrIdx": 1,
        "type": "primary"
    }
],
"status": "Created in J4",
}

And java code is used to find that job document using:

private DBCursor getRefMdCursor(String refId)
{
    DB db = JDatabase.getDatabase();
    BasicDBObject search = new BasicDBObject();
    search.put("refmdList._id", refId);

    DBCollection col = db.getCollection("jobs");
    DBCursor cur = col.find(search);
    return cur;
}

This is very slow and the refmdList._id is not indexed. Is the solution to index this array object field to speed it up or is there something problematic with the DBCursor code above?

DBCursor class is from mongodb/mongo-java-driver/3.4.3/mongo-java-driver-3.4.3.jar

1
How many records matches this query? Did you try .toArray() method? - Valijon

1 Answers

0
votes

An array's sub-document field can be indexed, and these indexes are called a Multikey indexes. See Index Arrays with Embedded Documents.

The field refmdList._id can be indexed. It can improve the performance of queries using that field as filter criteria.

About the Java code:

The MongoDB Java driver you are using is of older version (3.4.3), and there is a latest version available (3.12.0). Also, the Java code can be changed to use the newer APIs; see examples at Find Operations.