软件版本
- think-orm:3.0
- PHP:8.4.1
- MongoDB:8.0.4 (本地单数据 非集群)
- 注:我是在 webman 框架下使用think-orm,并非在 thinkphp框架下使用
使用场景
定义的模型如下:
<?php
namespace app\model;
use think\Model;
class User extends Model
{
protected $connection = 'mongodb_user';
//protected $table = 'user';
}
接收客户端的请求数据写入;写入的数据是一维PHP数组。安装 ThinkORM模型定义了一个模型,执行User::create($data)
得到的异常提示是:
Transaction numbers are only allowed on a replica set member or mongos
(如下方式未试)
$User->save($data);
解决方法
用模型的User::insertAll([$data])
执行数据写入,又可行。
不使用模型,直接使用如下方式也可行。
Db::connect('mongodb_user')->name('user')->save($data);
最终的选择在MongoDB
下是不使用模型
作为数据的写入。