Push操作
添加数据
public function add(){
$this->data = [
'作者' => 'tuzi',
'年龄' => '22',
'标题' => '防塔与补兵',
'评论' => [
[
'序号' => '001',
'内容' => '五杀'
]
]
];
$res = Db::table('document')->insert($this->data);
if($res){
echo "success";
}else{
echo "error";
}
}
结果
array(1) {
[0] => array(5) {
["_id"] => object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] => string(24) "5a51f73083869e4b65549c36"
}
["作者"] => string(4) "tuzi"
["年龄"] => string(2) "22"
["标题"] => string(15) "防塔与补兵"
["评论"] => array(1) {
[0] => array(2) {
["序号"] => string(3) "001"
["内容"] => string(6) "五杀"
}
}
}
}
利用push更新数据
public function update()
{
$update_data['评论'] = [
'$push',
[
'序号' => '002',
'内容' => '三杀'
]
];
$update_res = Db::table('document')->where('标题','防塔与补兵')->update($update_data);
if($update_res){
echo "success";
}else{
echo "error";
}
}
结果
array(1) {
[0] => array(5) {
["_id"] => object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] => string(24) "5a51f73083869e4b65549c36"
}
["作者"] => string(4) "tuzi"
["年龄"] => string(2) "22"
["标题"] => string(15) "防塔与补兵"
["评论"] => array(2) {
[0] => array(2) {
["序号"] => string(3) "001"
["内容"] => string(6) "五杀"
}
[1] => array(2) {
["序号"] => string(3) "002"
["内容"] => string(6) "三杀"
}
}
}
}