问题描述
现在,我可以找到"versions.0.content.axdducvoxb"带引号,但我不知道该怎么写parseFloat(data.versions.0.content.axdducvoxb)
Now, I can find 'versions.0.content.axdducvoxb' with quotes but i don't know how I have to write parseFloat(data.versions.0.content.axdducvoxb)
我尝试在MongoDB中使用代码进行字符串->数字转换.我这样做是为了使用聚合
I try to use code for string -> number conversions in MongoDB. I do this for using aggregate
有效:
db.documents.find().forEach(function(data) {
db.documents.update({_id:data._id},{$set:{axdducvoxb:parseFloat(data.axdducvoxb)}});
})
我有带有版本的数组,它不起作用:
I have array with versions, and it doesn't work:
> 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:意外的数字
I see error like this: 27 14:14:58.543 SyntaxError: Unexpected number
请告诉我我该怎么做)))
Please tell me what I have to do )))
第一个答案不起作用((((
如果我纠正查询并尝试:
If I correct query and try it:
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
如果我为两个索引都加上引号,则会清理基数:
If I add quotes for both indexes I clean the base:
db.documents.find().forEach(function(data) { db.documents.update({_id:data._id},{$set:{"versions.0.content.axdducvoxb":parseFloat("data.versions.0.content.axdducvoxb")}}}); })
我需要将所有带有数字的字符串转换为float)))
I need to transform all strings with numbers to float )))
我做到了!!!可能有人需要同样的东西....
我在mongo DB控制台中用于String到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";
?>
推荐答案
您应尝试将$ set值包装在引号中,如下所示:
You should try wrapping $set value in quotes as shown below:
"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)}});
})
每当使用此点符号时,都需要将键用引号引起来,如文档(强调):
Whenever you use this dot notation, you are required to wrap the key in quotes as mentioned in the documentation (emphasis added):
这篇关于Mongo DB数组将字符串更改为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!