8

我在 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
>       }
>     } 
>     }

我希望既能找到这份文件,又希望只提取model1.MondayModel.gtxdotdot.xdotdot/xdot/mean/sdvar...的值

我当前的代码使用以下代码:

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");                         

}

而不是运行思想他记录(如上)有没有办法使用点符号来获取值[model1.MondayModel.gtxdotdot.xdotdot]?例如:

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

3 回答 3

6

您可以通过以下三种方式之一进行操作。

您可以使用聚合框架使用点符号来投影嵌入字段的值。

使用聚合

 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");

使用不同的

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

使用查找

 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")
于 2018-04-11T00:47:08.320 回答
2

我认为您不能直接使用点表示法,但您可以创建自己的辅助函数。

解决方案 1:使用点表示法获取字段

public static Object getWithDotNotation( Document document, String dots ) 
     throws MongoException{

    String[] keys = dots.split( "\\." );
    Document doc = document;

    for( int i = 0; i < keys.length - 1; i++ ){
        Object o = doc.get( keys[ i ] );
        if( o == null || !( o instanceof Document ) ){
            throw new MongoException( String.format( 
                    "Field '%s' does not exist or s not a Document", keys[ i ] ) );
        }
        doc = ( Document ) o;
    }//end for

    return doc.get( keys[ keys.length - 1 ] );
}

然后你可以像这样使用它:

String dotNotation = "model1.MondayModel.gtxdotdot.xdotdot";

FindIterable<Document> projection = mongoColl.find()
    .projection( fields( include( dotNotation ) ) );

Double value = ( Double ) getWithDotNotation( projection.first(), dotNotation );

System.out.println( value ); // result: 0.0

这将大大简化您的代码。唯一需要注意的是:

  • 如果您不确定点符号,请使用try catch
  • 该方法getWithDotNotation可能返回 null
  • 小心强制转换,只有当你 100% 确定你的数据类型时才使用它(这里是 Double)。

解决方案 2:扁平化您的文档

public static Document flattenDoc( Document document ){

    Document flattened = new Document();
    Queue<Pair<String, Document>> queue = new ArrayDeque<>();
    queue.add( new Pair<>( "", document ) );

    while( !queue.isEmpty() ){
        Pair<String, Document> pair = queue.poll();
        String key = pair.getKey();
        for( Map.Entry<String, Object> entry : pair.getValue().entrySet() ){
            if( entry.getValue() instanceof Document ){
                queue.add( new Pair<>( key + entry.getKey() + ".", ( Document ) entry.getValue() ) );

            }else{
                flattened.put( key + entry.getKey(), entry.getValue() );

            }
        }//end for
    }

    return flattened;
}

使用您的示例数据,结果flattenDoc如下:

Document{{_id=569afce4b932c542500143ec,
date=2016-1-17T2:31:0Z,
day=17,
model1.date=2016-01-17T02:31+0000,
model1.MondayModel.gtxdotdot.xdotdot=0.0,
model1.MondayModel.gtxdotdot.xdot=0.0,
model1.MondayModel.lsxdotdot.xdotdot=0.0,
model1.MondayModel.lsxdotdot.xdot=0.0,
model1.MondayModel.gtxdot.xdotdot=0.0,
model1.MondayModel.gtxdot.xdot=0.0,
model1.MondayModel.lsxdot.xdotdot=0.0,
model1.MondayModel.lsxdot.xdot=0.0,
model1.MondayModel.modeldotdot.mean=0.0,
model1.MondayModel.modeldotdot.sdvar=0.0,
model1.MondayModel.modeldot.mean=0.0,
model1.MondayModel.modeldot.sdvar=0.0}}

所以可以getDouble("model1.MondayModel.gtxdotdot.xdotdot")直接使用。如果您需要访问所有字段,这种方法可能会更有效。

于 2016-01-30T09:15:50.717 回答
0

在最新版本的 MongoDB 和 Java 中,您可以使用where is your string path to the embedded value的getEmbedded()方法。以下速记的工作方式与上述完全相同。对于那些只在少数地方需要它并且不想编写整个函数的人很有用。Documentpath

String path = "model1.MondayModel.gtxdotdot.xdotdot";
Double value = document.getEmbedded(Arrays.stream(path.split("\\.")).toList(), Double.class);

我不确定这种方法的效率,但它肯定是更少的代码。

于 2021-12-16T19:23:40.340 回答