增
// 插入单条
// 文档地址:https://docs.mongodb.com/php-library/v1.8/reference/method/MongoDBCollection-insertOne/index.html
public function test()
{
$this->getAttribute()->insertOne([
"name" => "username",
"password" => "user_password",
]);
}
// 插入多条
// 文档地址:https://docs.mongodb.com/php-library/v1.8/reference/method/MongoDBCollection-insertMany/index.html
public function test()
{
$this->getAttribute()->insertMany([
[
"name" => "username",
"password" => "user_password",
],
[
"name" => "username02",
"password" => "user_password02",
]
]);
}
删除
// 最多删除一个符合过滤条件的文档。如果多个文档符合过滤条件,则只会 删除第一个匹配的文档。
// 使用文档:https://docs.mongodb.com/php-library/v1.8/reference/method/MongoDBCollection-deleteOne/index.html
public function test()
{
$this->getAttribute()->deleteOne([
"id" => 1,
"name" => "usernmae"
]);
}
// 删除多条
deleteMany 改一下关键词就行
更新
// 最多更新一个符合过滤条件的文档。如果多个文档符合过滤条件,则只会 更新第一个匹配的文档
// 使用文档:https://docs.mongodb.com/php-library/v1.8/reference/method/MongoDBCollection-updateOne/index.html
public function test()
{
$this->getAttribute()->updateOne([
"name" => "user_name"
], [
"name" => "user——name"
]);
}
// 更新多条 同样是
updateMany
查询
// 查找与查询匹配的单个文档。
// 文档地址: https://docs.mongodb.com/php-library/v1.8/reference/method/MongoDBCollection-findOne/index.html
OR查询 $condition 和$where差不都 都是键值对格式
public function test()
{
$condition= [
"key" => "value", // 键值对
"keyIn" => ['$in' => [1,2,3]], // WhereIn
"keyNotIn" => ['$not in' => [4,5,6]], // WhereNotIn
"keyNe" => ['$ne' => "7"] // !=
];
// -1 == asc 1 == desc
$options = [
"sort" => ["is_index_page" => -1, "sort_id" => 1, "id" => -1]
];
return $this->getAttribute()->find([
'$or' => [
$where_or,
[
'$and' => [
$condition
]
]
],
],$options);
}
// 单条和多条
find() == all
findOne() == first