$trunc

$trunc 运算符将数字截断到指定的小数位。

语法

{
  $trunc: [ <number>, <decimal place> ]
}

参数

参数 DESCRIPTION
<number> 要截断的数字。
<decimal place> 要截断指定数字的十进制位置。 正值截断小数点右侧,负值截断小数点左侧。

例子

请考虑存储集合中的此示例文档。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

示例 1 - 提取截断的位置坐标

若要检索“首次启动顾问”公司内商店的截断坐标,请首先运行查询以按公司名称筛选商店。 然后,使用纬度和经度字段上的 $trunc 运算符返回所需的结果。

db.stores.aggregate([
  {
    $project: {
      truncatedLat: { $trunc: ["$location.lat", 2] }
    }
  }
])

此查询返回的前三个结果为:

[
    {
        "_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
        "name": "First Up Consultants | Bed and Bath Pantry - Port Antone",
        "location": {
            "lat": 87.2239,
            "lon": -129.0506
        },
        "truncatedLatitute": 87,
        "truncatedLongitude": -129
    },
    {
        "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
        "name": "First Up Consultants | Microphone Bazaar - South Lexusland",
        "location": {
            "lat": -29.1866,
            "lon": -112.7858
        },
        "truncatedLatitute": -29,
        "truncatedLongitude": -112
    },
    {
        "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
        "name": "First Up Consultants | Plumbing Supply Shoppe - New Ubaldofort",
        "location": {
            "lat": -0.2136,
            "lon": 108.7466
        },
        "truncatedLatitute": 0,
        "truncatedLongitude": 108
    }
]