簡體   English   中英

在MongoDB中查詢嵌入式文檔

[英]Querying an embedded document in MongoDB

我有一個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聚合框架https://docs.mongodb.com/manual/reference/operator/aggregation/

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM