我正在尝试将图像上传到现有文档。(通过添加一个字段。)我正在通过电子邮件输入找到我的文档。这是我的代码:-
<?php
$errors = array();
$data = array();
$_POST = json_decode(file_get_contents('php://input'), true);
$name=$__POST["mn"];
$email=$__POST["me"];
$i2u=$_POST["image2upload"];
$binary = base64_decode($i2u);
$filepath = 'images/newuploads/'.$name.'/'.$name.'.jpg';
$imgurl = 'http://localhost/portfolio3/php/'.$filepath;
file_put_contents($filepath, $binary);
$m = new MongoClient();
$db = $m->mydb2->mycol2;
$foundRecord = $db->find(array('Email'=>$email));
$foundRecord->update(array(‘$set’ => array(‘uploadpic1’ => new MongoBinData(file_get_contents($imgurl)))));
?>
如何使用php在MongoDB文档中添加一个字段?
首先,如果您仅搜索一条记录,则应使用 $db->findOne
然后,您可以执行以下操作:
$foundRecord = $db->findOne(array('Email'=>$email));
$foundRecord['uploadpic1'] = new MongoBinData(file_get_contents($imgurl);
$db->save($foundRecord);
因为您使用过findOne
,它将保存到提取的记录中,而不是在使用时创建新记录$db->save($foundRecord);
否则,如果要使用,则update
需要在其中指定查询:
$db->update(array('Email'=>$email), array('$set' => array('uploadpic1' => new MongoBinData(file_get_contents($imgurl)))));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句