簡體   English   中英

MongoDB-FindOne將數據類型從NumberInt更改為十進制

[英]MongoDB - FindOne changes data type from NumberInt to decimal

我必須使用集合中的元素:

數據:

{ 
        "_id" : ObjectId("599ccef160b543xbf380c99x"), 
        "code" : "ME0001", 
        "description" : "Element 1", 
        "operationCode" : null, 
        "order" : NumberInt(1), 
        "level" : NumberInt(0), 
        "subItems" : [
        ]
    }

當我運行此查詢以獲取數據時:

var obj = db.itemMenu.findOne({"code": "ME0001"});

問題是,“訂單”和“級別”字段的數據類型隨您所見而變化:

{ 
    "_id" : ObjectId("599ccef160b543xbf380c99x"), 
    "code" : "ME0001", 
    "description" : "Element 1", 
    "operationCode" : null, 
    "order" : 1.0, 
    "level" : 0.0, 
    "subItems" : [
    ]
}

如何獲取不變的數據?

使用模式僅允許整數: https : //docs.mongodb.com/manual/core/schema-validation/

要將所有浮點數更新為整數:

db.itemMenu.find( {  } ).forEach( function (item) {   
  item.order = parseInt(item.order); // convert field to int
  item.level = parseInt(item.level); // convert field to int
  db.itemMenu.save(item);
});

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM