Mongodb将Documents数组放在同一级别

时间:2017-10-31 17:54:40

标签: mongodb aggregate

我有这个文档数组,我想把“table”放在同一级别,如mastil_antenas和其他变量。我怎么能用聚合做到这一点?

我正在尝试使用聚合$项目,但我无法得到结果。

数据示例

[ {
    "mastil_antena" : "1",
    "nro_platf" : "1",
    "antmarcmast" : "ANDREW",
    "antmodelmast" : "HWXXX6516DSA3M",
    "retmarcmast" : "Ericsson",
    "retmodelmast" : "ATM200-A20",
    "distmast" : "1.50",
    "altncramast" : "41.30",
    "ORIENTMAG" : "73.00",
    "incelecmast" : "RET",
    "incmecmast" : "1.00",
    "Feedertypemast" : "Fibra Optica",
    "longjumpmast" : "5.00",
    "longfo" : "100",
    "calibrecablefuerza" : "10 mm",
    "longcablefuerza" : "65.00",
    "modelorruantena" : "32B66A",
    "tiltmecfoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114934746000.jpg",
    "tiltmecfoto_fh" : "2017-10-18T05:51:22Z",
    "az0foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115012727000.jpg",
    "az0foto_fh" : "2017-10-18T05:55:21Z",
    "azneg60foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115016199000.jpg",
    "azneg60foto_fh" : "2017-10-18T05:55:36Z",
    "azpos60foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115020147000.jpg",
    "azpos60foto_fh" : "2017-10-18T05:55:49Z",
    "etiqantenafoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114920853000.jpg",
    "etiqantenafoto_fh" : "2017-10-18T05:56:01Z",
    "tiltelectfoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114914236000.jpg",
    "tiltelectfoto_fh" : "2017-10-18T05:56:13Z",
    "idcablefoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114900279000.jpg",
    "idcablefoto_fh" : "2017-10-18T05:56:38Z",
    "rrutmafoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114947279000.jpg",
    "rrutmafoto_fh" : "2017-10-18T05:56:49Z",
    "etiquetarrufoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114954648000.jpg",
    "etiquetarrufoto_fh" : "2017-10-18T05:57:02Z",
    "rrutmafoto1" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114959738000.jpg",
    "rrutmafoto1_fh" : "2017-10-18T05:57:12Z",
    "etiquetarrufoto1" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115005545000.jpg",
    "etiquetarrufoto1_fh" : "2017-10-18T05:57:27Z",
    "botontorre4" : "sstelcel3",
    "table" : { /* put all varibles one level up*/
        "tecmast" : "LTE",
        "frecmast" : "2100",
        "secmast" : "1",
        "untitled440" : "Salir"
    },
    "comentmast" : "",
    "longfeedmast" : "",
    "numtmasmast" : "",
    "otra_marca_antena" : "",
    "otro_modelo_antena" : ""
}]

1 个答案:

答案 0 :(得分:1)

从MongoDB版本3.4开始,您可以使用$addFields来执行此操作。

//replace products with what makes sense in your database
db.getCollection('products').aggregate(
[
    {   //1 add the properties from subdocument table to documents
        $addFields: {
        "documents.tecmast" : "documents.0.table.tecmast",
            "documents.frecmast" : "documents.0.table.frecmast",
            "documents.secmast" : "documents.0.table.secmast",
            "documents.untitled440" : "documents.0.table.untitled440"
    }
    },
    {
        //(optional) 2 remove the table property from the documents
        $project:  {"documents.table" : 0}        
    }
]
)

步骤1:使用$ addFields从documents.table中的表中获取属性并将它们放在文档上

第2步:(可选)删除属性" table"来自文件。

我希望这会有所帮助!!!

相关问题