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

该文介绍了一个MongoDB查询案例,旨在从数据库中提取特定用户的消费详情,包括不同类型的资源(如COMPUTE、STORAGE、TRAFFIC)在各地区的成本分布。查询结果展示了简化后的资源消费结构,便于快速了解用户在不同区域的费用概况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值