mongodb查询出指定的需要的字段

x33g5p2x  于2022-05-11 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(222)

mongodb的数据结构如下:

{
    "_id" : ObjectId("61d569a96cf0454e7b832218"),
    "billingCycle" : {
        "year" : 2022,
        "month" : 1
    },
    "tenantId" : NumberLong(1432300453542105088),
    "userId" : NumberLong(15),
    "resources" : [ 
        {
            "type" : "COMPUTE",
            "regions" : [ 
                {
                    "region" : "north1",
                    "platforms" : [ 
                        {
                            "platform" : "WINDOWS",
                            "usages" : [ 
                                {
                                    "model" : "p1.c1.2",
                                    "price" : "0.627100",
                                    "usage" : "0.1156",
                                    "cost" : "0.0725"
                                }
                            ]
                        }, 
                        {
                            "platform" : "LINUX",
                            "usages" : [ 
                                {
                                    "model" : "p1.c1.2",
                                    "price" : "0.627100",
                                    "usage" : "3780.0000",
                                    "cost" : "2370.4380"
                                }
                            ]
                        }
                    ],
                    "usages" : [ 
                        {
                            "model" : "p1.c1.2",
                            "price" : "0.627100",
                            "usage" : "3780.1156",
                            "cost" : "2370.5105"
                        }
                    ],
                    "cost" : "2370.5105"
                }, 
                {
                    "region" : "ap2",
                    "platforms" : [ 
                        {
                            "platform" : "WINDOWS",
                            "usages" : [ 
                                {
                                    "model" : "s2.c1.2",
                                    "price" : "0.714200",
                                    "usage" : "0.2567",
                                    "cost" : "0.1834"
                                }
                            ]
                        }, 
                        {
                            "platform" : "LINUX",
                            "usages" : [ 
                                {
                                    "model" : "s2.c1.2",
                                    "price" : "0.313500",
                                    "usage" : "1.3789",
                                    "cost" : "0.4323"
                                }
                            ]
                        }
                    ],
                    "usages" : [ 
                        {
                            "model" : "s2.c1.2",
                            "price" : "0.313500",
                            "usage" : "1.6356",
                            "cost" : "0.6156"
                        }
                    ],
                    "cost" : "0.6156"
                }
            ],
            "cost" : "2371.1261"
        }, 
        {
            "type" : "STORAGE",
            "regions" : [ 
                {
                    "region" : "north1",
                    "platforms" : [],
                    "usages" : [ 
                        {
                            "model" : "STORAGE",
                            "price" : "0.675600",
                            "usage" : "115.8058",
                            "cost" : "78.2416"
                        }
                    ],
                    "cost" : "78.2416"
                }, 
                {
                    "region" : "ap2",
                    "platforms" : [],
                    "usages" : [ 
                        {
                            "model" : "STORAGE",
                            "price" : "0.337800",
                            "usage" : "0.1136",
                            "cost" : "0.0384"
                        }, 
                        {
                            "model" : "IMAGE",
                            "price" : "0.176200",
                            "usage" : "0.0021",
                            "cost" : "0.0004"
                        }
                    ],
                    "cost" : "0.0388"
                }
            ],
            "cost" : "78.2804"
        }, 
        {
            "type" : "TRAFFIC",
            "regions" : [ 
                {
                    "region" : "north1",
                    "platforms" : [],
                    "usages" : [ 
                        {
                            "model" : "DOWNLOAD",
                            "price" : "1.186800",
                            "usage" : "0.0006",
                            "cost" : "0.0008"
                        }
                    ],
                    "cost" : "0.0008"
                }, 
                {
                    "region" : "ap2",
                    "platforms" : [],
                    "usages" : [ 
                        {
                            "model" : "DOWNLOAD",
                            "price" : "0.593400",
                            "usage" : "0.0001",
                            "cost" : "0.0001"
                        }
                    ],
                    "cost" : "0.0001"
                }
            ],
            "cost" : "0.0009"
        }
    ],
    "totalCost" : "2449.41",
    "createdAt" : {
        "dateTime" : ISODate("2022-01-05T09:49:29.182Z"),
        "offset" : "Z"
    },
    "createdTime" : NumberLong(1641376169000),
    "_class" : "com.fastonetech.billing.domain.bill.v1.BillV1"
}

针对上面的数据,只想统计出具体的消费情况。比如:

mongodb的查询语句:

db.getCollection('bills_v1').find({"tenantId":1461884485267230720,"userId":213},{ billingCycle:1, totalCost: 1,resources:{type:1,cost:1,regions:{region:1,cost:1}} } )

{ billingCycle:1, totalCost: 1,resources:{type:1,cost:1,regions:{region:1,cost:1}}

需要那些字段,需要在字段的后面加上1,比如billingCycle:1
当涉及到嵌套的情况,可以使用{ 字段名:{字段名:1,字段名:1} }
如果涉及到多层嵌套的情况,可以使用{字段名:{字段名:1,字段名:{字段名:1,字段名:1}}}

查询出来的出来的结果精简为:

{
    "_id" : ObjectId("62674f032366603fb958ac9c"),
    "billingCycle" : {
        "year" : 2022,
        "month" : 4
    },
    "resources" : [ 
        {
            "type" : "STORAGE",
            "regions" : [ 
                {
                    "region" : "ap2",
                    "cost" : "0.5047"
                }, 
                {
                    "region" : "north1",
                    "cost" : "0.5930"
                }, 
                {
                    "region" : "north3",
                    "cost" : "0.0503"
                }
            ],
            "cost" : "1.1480"
        }, 
        {
            "type" : "COMPUTE",
            "regions" : [ 
                {
                    "region" : "ap2",
                    "cost" : "39.6336"
                }, 
                {
                    "region" : "north1",
                    "cost" : "34.1209"
                }, 
                {
                    "region" : "north3",
                    "cost" : "0.2985"
                }
            ],
            "cost" : "74.0530"
        }, 
        {
            "type" : "TRAFFIC",
            "regions" : [ 
                {
                    "region" : "ap2",
                    "cost" : "0.0003"
                }, 
                {
                    "region" : "north1",
                    "cost" : "0.0095"
                }
            ],
            "cost" : "0.0098"
        }
    ],
    "totalCost" : "75.22"
}

相关文章

微信公众号

最新文章

更多