我不知道如何在MongoDB中舍入一个数字.我只发现如何使用2位小数,但没有更多的小数.
"location" : {
"type" : "Point",
"coordinates" : [
-74.00568,
40.70511
]
}
Run Code Online (Sandbox Code Playgroud)
这是一个坐标的例子,我需要在点之后用3个数字进行舍入.谢谢
对于 3 位小数舍入,您可以使用此公式。
$divide: [ {$trunc: { $multiply: [ "$$coordinate" , 1000 ] } }, 1000 ]
例如,使用您的示例数据,并使用此聚合:
db.getCollection('Test2').aggregate([
{ $project :
{
"location.type" : "$location.type",
"location.coordinates" :
{
$map:
{
input: "$location.coordinates",
as: "coordinate",
in: { $divide: [ {$trunc: { $multiply: [ "$$coordinate" , 1000 ] } }, 1000 ] }
}
}
}
}
])
Run Code Online (Sandbox Code Playgroud)
您可以获得所需的结果。
{
"_id" : ObjectId("59f9a4c814167b414f6eb553"),
"location" : {
"type" : "Point",
"coordinates" : [
-74.005,
40.705
]
}
}
Run Code Online (Sandbox Code Playgroud)