【问题标题】:Querying an embedded document in MongoDB查询 MongoDB 中的嵌入文档
【发布时间】:2018-03-20 15:36:06
【问题描述】:

我有一个 Bson,其中包含一些遵循以下结构的嵌入文档。我想知道如何获取特定日期的变量值(“2014-08-09”:53.0)或库存值(“2014-08-09”:0.0) .

{
    "_id" : ,
    "last_modified" : "",
    "client" : "",
    "credentials" : [],
    "tags" : [],
    "variables" : [ 
        {
            "name" : "sales",
            "_values" : {
                "2014-08-09" : 53.0,
                ...
            },
            "tags" : [],
            "is_target" : true,
            "method1" : "",
            "method2" : "",
            "vtype" : ,
            "index_type" : 
        }, 
        {
            "name" : "inventory",
            "_values" : {
                "2014-08-09" : 0.0,
                ...
            },
            "tags" : [],
            "is_target" : ,
            "method1" : "",
            "method2" : "",
            "vtype" : ,
            "index_type" : 
        }, 
        {
            "name" : "price",
            "_values" : {},
            "tags" : [],
            "is_target" : ,
            "method2" : "",
            "method1" : "",
            "vtype" : ,
            "index_type" : 
        }
    }
} 

【问题讨论】:

    标签: mongodb mongodb-query bson


    【解决方案1】:

    尝试使用 mongodb 聚合框架 https://docs.mongodb.com/manual/reference/operator/aggregation/

    db.mycol.aggregate([
        {"$unwind":"$variables"},
        {$project:{"date":"$variables._values.2014-08-09"}}
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 2015-02-06
      • 2015-08-30
      • 2021-08-07
      • 2023-03-08
      • 1970-01-01
      • 2018-03-09
      相关资源
      最近更新 更多