Yii2中MongoDB的使用方法-CURD

网上资料很少,查看官方文档后整理的一些最基本的MongoDB的CURD与聚合方法

创建一个Model和Cortroller进行测试,代码如下:

Model

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

<?php

/*

 * http://www.yiiframework.com/

 * */

namespace app\modules\index\models;

use yii\mongodb\Query;

use yii\mongodb\ActiveRecord;

use yii\data\ActiveDataProvider;

class Customer extends ActiveRecord

{

    //指定集合名称

    public static function collectionName()

    {

        return 'runoob';

    }

    //定义字段属性

    public function attributes()

    {

        return [

            '_id',

            'title',

            'description'

        ];

    }

    //添加文档方法1

    public static function insertInfo(){

        $collection = \Yii::$app->mongodb->getCollection (Customer::collectionName());

        //返回添加后的objectId,不限制于attributes

        $res $collection->insert([

            'name' => 'John Smith22',

            'status' => 2

        ]);

        return $res;

    }

    //添加文档方法2,限制于attributes

    public static function saveInfo(){

        $customer new Customer();

        $customer->title = '111';

        $customer->description = '222';

        $customer->insert();

        //toArray() 将对象转为数组,返回本次添加的数据JSON

        return $customer->toArray();

    }

    //删除文档

    public static function deleteInfo(){

        //返回删除的记录数

        $res = Customer::deleteAll([

            '_id' => '58ddf84f36420258358b4567'

        ]);

        return $res;

    }

    //编辑文档 - [set],[where] @修改符合条件的所有记录,没有title字段则创建

    public static function updateInfo(){

        //返回更新的条数

        $res = Customer::updateAll(

            ['title' => 'gg','description' => 'nn',],

                ['_id' => '58ddf8653642026c148b4567']

        );

        return $res;

    }

    //编辑文档 - 在所有文档中增加 age & ccy 如存在则会+1

    public static function countersInfo(){

        //返回更新的条数

        $res = Customer::updateAllCounters(['age' => 1,'ccy' => 3]);

        return $res;

    }

    //查询文档 - 聚合查询

    public static function cmsInfo(){

        $query new Query ();

        $row $query->select(['title','url'])

            ->from ( Customer::collectionName() )

            ->where(['title' => 'test'])

            ->average('age');//age的平均值

            //->max('age');//age最大的值

            //->min('age');//age最小的值

            //->sum('ccy');//ccy所有列相加的和

            //->count('_id');//当前数据总数

        return $row;

    }

    //查询文档 - 可用于分页列表

    public static function selectInfo(){

        $query new Query ();

        $query->select(['title','description'])

            ->from (Customer::collectionName())

            ->offset ( 4 )

            ->limit ( 2 );

        $rows $query->all();

        return $rows;

    }

    //查询文档 - 根据条件只查询一条数据

    public static function oneInfo(){

        $query new Query ();

        $row $query->select(['title','url'])

            ->from ( Customer::collectionName() )

            ->where(['_id' => '58ddf8653642026c148b4567'])

            ->one ();

        return $row;

    }

    //ActiveDataProvider 方式查询1

    public static function adp1Info(){

        $provider new ActiveDataProvider ( [

            'query' => Customer::find (),

            'pagination' => [

                'totalCount' => 10,

                'pageSize' => 2,

                'page' => 4

            ]//可用于分页

        ] );

        $models $provider->getModels ();

        return $models;

    }

    //ActiveDataProvider 方式查询2

    public static function adp2Info(){

        $query new Query ();

        $query->select(['title'])->from ( Customer::collectionName() )->where ( [

            'title' => 'test'

        ] );

        $provider new ActiveDataProvider ( [

            'query' => $query,

            'pagination' => [

                'totalCount' => 10,

                'pageSize' => 2,

                'page' => 2

            ]//可用于分页

        ] );

        $models $provider->getModels ();

        return $models;

    }

}

Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?php

namespace app\modules\index\controllers;

use yii\web\Controller;

use app\modules\index\models\Customer;

class MongoController extends Controller

{

    public function actionIndex(){

        $res = Customer::cmsInfo();

        var_dump ( $res );

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程工人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值