2017-10-31 46 views
4

MongoDBで数値を丸める方法がわかりません。私は2小数点ではそれを行う方法を見つけるが、小数点以下では見つけることはできません。MongoDB - 3桁の小数点以下を切り捨てる方法

"location" : { 
    "type" : "Point", 
    "coordinates" : [ 
     -74.00568, 
     40.70511 
    ] 
} 

これは、ドットの後に3つの数字で丸めなければならない座標の例です。 ありがとうございます

+0

丸めたり切り捨てたりしますか? –

+0

@JasonVを切り捨てたい –

+0

[流星群、mongodb、スペースバー、2小数点以下を表示するにはどうすればいいですか?](https://stackoverflow.com/questions/27577891/meteor-mongodb-spacebars-how-do- i-display-only-2-decimal-places) – FabioBranch

答えて

1

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 ] } 
       } 
      } 
     } 
    } 
]) 

あなたが所望の結果を得ることができます。

{ 
    "_id" : ObjectId("59f9a4c814167b414f6eb553"), 
    "location" : { 
     "type" : "Point", 
     "coordinates" : [ 
      -74.005, 
      40.705 
     ] 
    } 
} 
関連する問題