Get value from a object

99 views Asked by At

my collection contains documents like this:

{
    "_id": ObjectId("585a7886e4b06aec5d1d1639"),
    "name": "somename",
    "owner": "someowner",
    "slots": 50,
    "gold": 0,
    "tag": "sometext",
    "motd": "sometext",
    "purchases": [],
    "members": {
        "membername1": {
            "rank": 2
        },
        "membername2": {
            "rank": 5
        },
        "membername3": {
            "rank": 3
        }
    }
}

I need to get int value from "members.membername.rank". how do I get this value, knowing only membername?

1

There are 1 answers

0
Muhammad Gelbana On

The following code excludes the document's ID and gets only the rank for memername1

List<Document> pipeline = Arrays.asList(new Document("$project", new Document("rank", "$members.membername1.rank").append("_id", false)));
return ApiResponse.withBody(coll.aggregate(pipeline).first().getInteger("rank"));