Mongodb数组将字符串更改为float

时间:2014-03-31 12:21:44

标签: mongodb

现在,我可以找到&version; .content.axdducvoxb' 有引号,但我不知道如何写 parseFloat(data.versions.0.content.axdducvoxb)

我尝试将代码用于字符串 - > MongoDB中的数字转换。我这样做是为了使用聚合

有效:

db.documents.find().forEach(function(data) {
    db.documents.update({_id:data._id},{$set:{axdducvoxb:parseFloat(data.axdducvoxb)}});
})

我有版本的数组,它不起作用:

>     db.documents.find().forEach(function(data) {
>     db.documents.update({_id:data._id},{$set:{versions.0.content.axdducvoxb:parseFloat(data.versions.0.content.axdducvoxb)}});
> })

我看到这样的错误:27 14:14:58.543 SyntaxError:意外的数字

请告诉我我必须做什么)))

第一个答案不起作用((

如果我更正查询并尝试:

db.documents.find().forEach(function(data) {   db.documents.update({_id:data._id},{$set:{"versions.0.content.axdducvoxb":parseFloat(data.versions.0.content.axdducvoxb)}}}); })
    Mon Mar 31 13:23:45.474 SyntaxError: Unexpected number

如果我为两个索引添加引号,我会清理基数:

db.documents.find().forEach(function(data) {   db.documents.update({_id:data._id},{$set:{"versions.0.content.axdducvoxb":parseFloat("data.versions.0.content.axdducvoxb")}}}); })

我需要用数字转换所有字符串以浮动)))

我做到了!可能有人需要一样......

我在mongo数据库控制台中进行String to Float转换的代码(4个示例):

db.documents.find({ 'fwtweaeeba' : {$exists : true}}).forEach( function(obj) { obj.fwtweaeeba = parseFloat( obj.fwtweaeeba ); db.documents.save(obj); } ); 

db.documents.find({ 'versions.0.content.fwtweaeeba' : {$exists : true}}).forEach( function(obj) { obj.versions[0].content.fwtweaeeba = parseFloat( obj.versions[0].content.fwtweaeeba ); db.documents.save(obj); } ); 

db.documents.find({ 'versions.1.content.fwtweaeeba' : {$exists : true}}).forEach( function(obj) { obj.versions[1].content.fwtweaeeba = parseFloat( obj.versions[1].content.fwtweaeeba ); db.documents.save(obj); } ); 

db.documents.find({ 'versions.2.content.fwtweaeeba' : {$exists : true}}).forEach( function(obj) { obj.versions[2].content.fwtweaeeba = parseFloat( obj.versions[2].content.fwtweaeeba ); db.documents.save(obj); } );

PHP脚本将字符串转换为在集合中浮动(我需要的所有字符串):

<?php
$fieldNamesToFloat = array(
         "atkswlntfd",
         "auwbsjqzir",
         "avqrnjzbgd",
         "awquatbduv",
         "axdducvoxb",
         "bkoldugcrp",
         "bzccjslewi",
         "cclwyezydc",
         "covfctuuhi",
         "dingrnyknr",
         "dkfknpzsnt",
         "dqetuhllse",
         "duorlxiqbw",
         "eayoicezsh",
         "esrfffruoy",
         "ewdunlkxue",
         "ewfshjnome",
         "exakqiudxg",
         "fabdcybqxu",
         "fsplxunmaf",
         "fubxmogyam",
         "fuzqrnwsks",
         "fwtweaeeba",
         "fybnnlojgb",
         "gdjheqrqcx",
         "gpupstzwjd",
         "gxilphzzcu",
         "hbahrruokf",
         "hbqnleclwp",
         "hchpoxxnwz",
         "hmorfnbfvf",
         "hqatnzynxb",
         "hrqssioxdv",
         "hvscavwupe",
         "hyzlbtnxil",
         "idzxqjoxgd",
         "ikxsvguboy",
         "ipjpwkbqnt",
         "izqighabwk",
         "jncncbplme",
         "jxkspszlrc",
         "kekarveuhb",
         "klyoglzriu",
         "kmvuelmdwe",
         "knxzfjwnax",
         "kqfhjboecc",
         "kqhojbwvmo",
         "lchogmhynm",
         "lmuwyeqvph",
         "lvcdbhisbx",
         "mijczjeqeo",
         "mpxzquzcat",
         "mqqoetqued",
         "muktdrzphw",
         "nceszojuvt",
         "nypnjqgxop",
         "ojklibfieg",
         "padotysmxb",
         "pidwyruvfq",
         "pkeymzxsrj",
         "pnjtfvzwiv",
         "pqjnpoxmcx",
         "pyexnkjujx",
         "qfeqdvzssg",
         "qidslfqnwn",
         "qvjszkahdc",
         "qzoriqedoh",
         "rjicuyfsmt",
         "rqenalbuko",
         "rxkwogbxwu",
         "sbqqabqukn",
         "skhgbmucrp",
         "slewjrvgjn",
         "tidjarsatz",
         "tmbagkmlgb",
         "uqcdessbeu",
         "vacoxpronz",
         "vdjfydjrpa",
         "vktxndqyhm",
         "vmfqvfjptu",
         "vofeebpgsc",
         "wewmtafjvk",
         "wfqdcsrltv",
         "wzmbxalguv",
         "xjllpdyool",
         "xjxbwynytx",
         "xmirihwycl",
         "xxrsnjlmfv",
         "ybgdjpexth",
         "ymkmkuxlrq",
         "yneoycjloj",
         "yogujlfvpb",
         "zahigfmqxb",
         "znqqhqkjte",
         "zyztudtziu"
);
$m = new MongoClient();
$db = $m->foo_data;
$collection = new MongoCollection($db, 'documents');
foreach($fieldNamesToFloat as $fieldName){
    foreach($db->documents->find(array("type" => "chair")) as $document){
        $db->documents->update(
            array('_id' => $document[_id]),
            array(
                '$set' => array(
                    'versions.0.content.'.$fieldName => (float)$document['versions'][0]['content'][$fieldName],
                    'versions.1.content.'.$fieldName => (float)$document['versions'][1]['content'][$fieldName],
                    'versions.2.content.'.$fieldName => (float)$document['versions'][2]['content'][$fieldName],
                    $fieldName => (float)$document[$fieldName]
                )
            ),
            array('$multi' => true)
        );
    }
}
echo "well done";      
?> 

1 个答案:

答案 0 :(得分:3)

您应该尝试将$ set值包装在引号中,如下所示:

"versions.0.content.axdducvoxb":parseFloat(data.versions.0.content.axdducvoxb)

最终结果:

db.documents.find().forEach(function(data) {
  db.documents.update({_id:data._id},{$set:"versions.0.content.axdducvoxb":parseFloat(data.versions.0.content.axdducvoxb)}});
})

每当您使用此点符号时,您需要将键包装在引号中,如the documentation中所述(强调添加):

  

要通过从零开始的索引位置访问数组的元素,请将数组名称与点(。)和从零开始的索引位置连接,并用引号括起来