问题
I tried make it from mongodb console, but it doesn't work, because I can not work with arrays like afrray[0] afrray[1] afrray[2] etc
How to change array elements
Now I try to do it from php, but i see only zeros in base ((((
What do I wrong?
$out = $collection->update(
array("type" => 'chair'), array('$set' => array( 'vmfqvfjptu' => floatval('vmfqvfjptu'),
'versions.0.content.vmfqvfjptu'=> floatval('versions.0.content.vmfqvfjptu'),
'versions.1.content.vmfqvfjptu'=> floatval('versions.1.content.vmfqvfjptu'),
'versions.2.content.vmfqvfjptu'=> floatval('versions.2.content.vmfqvfjptu'),
'versions.3.content.vmfqvfjptu'=> floatval('versions.3.content.vmfqvfjptu')
)),
array("multiple" => true)
);
How can I send string from database near floatval('versions.0.content.vmfqvfjptu') ?
This script works good from mongo console. There are 4 scripts. Every one takes string from database, convert string to float and save float to database (for each documents in collection).
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); } );
I need make the same with php because I have a lot of fields in every document to convert...
It works !!!! Thank you, Sammaye !!! You really helped me!!!
foreach($db->documents->find(array("type" => "chair")) as $document){
// my_dump($document[_id]);
$db->documents->update(
array('_id' => $document[_id]),
array(
'$set' => array(
'versions.0.content.axdducvoxb' => (float)$document['versions'][0]['content']['axdducvoxb'],
'versions.1.content.axdducvoxb' => (float)$document['versions'][1]['content']['axdducvoxb'],
'versions.2.content.axdducvoxb' => (float)$document['versions'][2]['content']['axdducvoxb'],
'axdducvoxb' => (float)$document['axdducvoxb']
)
),
array('$multi' => true)
);
}
回答1:
Following on from the question you linked:
db.documents.find({"type": "chair"}).forEach(function(data) {
db.documents.update({_id:data._id},
{$set:{'versions.0.content.axdducvoxb':
parseFloat(data.versions.0.content.axdducvoxb)}});
})
Would be:
foreach($db->documents->find(array("type" => "chair")) as $document){
$db->documents->update(
array('_id' => $document['_id']),
array(
'$set' => array(
'versions.0.content.axdducvoxb' => floatval($document['versions'][0]['content']['axdducvoxb'])
)
)
);
}
There done. good answer
回答2:
For php, this
floatval('versions.0.content.vmfqvfjptu')
evaluates to false. Because it's not the value of the field from the db's collection
来源:https://stackoverflow.com/questions/22810612/mongodb-php-strings-to-float-values