Mongo使用文档

// 插入单条
// 文档地址: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值