Get value from Embedded Document Mongo Java

DevilCode picture DevilCode · Jan 26, 2016 · Viewed 8k times · Source

I have the following document in mongo:

>  {    "_id": ObjectId("569afce4b932c542500143ec"),    
>    "date": "2016-1-17T2:31:0Z",    
>    "day": NumberInt(17),    
>    "model1": {
>      "date": "2016-01-17T02:31+0000",
>      "MondayModel": {
>        "gtxdotdot": {
>          "xdotdot": 0,
>          "xdot": 0
>       },
>        "lsxdotdot": {
>          "xdotdot": 0,
>          "xdot": 0
>       },
>        "gtxdot": {
>          "xdotdot": 0,
>          "xdot": 0
>       },
>        "lsxdot": {
>          "xdotdot": 0,
>          "xdot": 0
>       },
>        "modeldotdot": {
>          "mean": 0,
>          "sdvar": 0
>       },
>        "modeldot": {
>          "mean": 0,
>          "sdvar": 0
>       }
>     } 
>     }

I wish to both find this document and extract only the values of model1.MondayModel.gtxdotdot.xdotdot/xdot/mean/sdvar ...

My current code does so with the following:

MongoCursor<Document>  back = collection.find(and(eq("topic",topic),eq("sp",sp))).limit(1).iterator();
if (back.hasNext())
{
    Document doc = back.next();
    Document tmpddc1 = (Document)doc.get("model1");
    Document tmpddc2 = (Document)tmpddc1.get("MondayModel");

    Document tmpddc3 = (Document)tmpddc2.get("gtxdotdot");
    gtxdotdotXdotdot = tmpddc3.getDouble("xdotdot");
    gtxdotdotXdot    = tmpddc3.getDouble("xdot");

             tmpddc3 = (Document)tmpddc2.get("lsxdotdot");
    lsxdotdotXdotdot = tmpddc3.getDouble("xdotdot");
    lsxdotdotXdot    = tmpddc3.getDouble("xdot");

             tmpddc3 = (Document)tmpddc2.get("gtxdot");
    gtxdotXdotdot = tmpddc3.getDouble("xdotdot");
    gtxdotXdot    = tmpddc3.getDouble("xdot");            

             tmpddc3 = (Document)tmpddc2.get("lsxdot");
    lsxdotXdotdot = tmpddc3.getDouble("xdotdot");
    lsxdotXdot    = tmpddc3.getDouble("xdot");  


             tmpddc3 = (Document)tmpddc2.get("modeldotdot");
    modeldotdotXmean = tmpddc3.getDouble("mean");
    modeldotdotXsdvar    = tmpddc3.getDouble("sdvar");             

             tmpddc3 = (Document)tmpddc2.get("modeldot");
    modeldotXmean = tmpddc3.getDouble("mean");
    modeldotXsdvar    = tmpddc3.getDouble("sdvar");                         

}

Instead of running thought he document (as above) is there a way to get the values using the dot notation [model1.MondayModel.gtxdotdot.xdotdot] ? Something such as:

double value =  doc.getDouble("model1.MondayModel.gtxdotdot.xdotdot");

Answer

s7vr picture s7vr · Apr 11, 2018

You can do it one of three ways.

You can use aggregation framework to project the value of embedded field using dot notation.

Using Aggregation

 import static com.mongodb.client.model.Aggregates.*;
 import static com.mongodb.client.model.Filters.eq;
 import static com.mongodb.client.model.Projections.computed;
 import static java.util.Arrays.*;
 import static com.mongodb.client.model.Projections.include;

 MongoClient mc = new MongoClient();
 MongoDatabase db = mc.getDatabase("test");
 MongoCollection<Document> collection = db.getCollection("collection");
 Document document = 
     collection.aggregate(asList(
        match(eq("day",17)),
        project(computed("val", "$model1.MondayModel.gtxdotdot.xdotdot")))).
     first();
 Double embeddedField = document.getDouble("val");

Using Distinct

 Double embeddedField = collection.distinct("model1.MondayModel.gtxdotdot.xdotdot", eq("day",17), Double.class).first();

Using Find

 Document document = collection.find(eq("day",17)).projection(include("model1.MondayModel.gtxdotdot.xdotdot")).first();
 Double embeddedField = document.get("model1", Document.class).get("MondayModel", Document.class).get("gtxdotdot", Document.class).getDouble("xdotdot")