Php-implemented mongodb operation class _ PHP Tutorial

Source: Internet
Author: User
Tags findone
Mongodb operations implemented by php. When talking about php connection to mongodb, the mongoDB operation class implemented by php has to introduce the official php manual at: us. php. netmanualenbook. mongo. php: next I will share with you a mongodb operation class implemented by php.

Speaking of php connection to mongoDB, I have to first introduce the official php Manual.

Pai_db.php

?

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

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

/**

* Created by PhpStorm.

* User: yangyulong

* Date: 2015/5/26

* Time: 13: 45

*/

Class pai_db

{

Private static $ instanceof = NULL;

Public $ mongo;

Private $ host = 'localhost ';

Private $ port = '000000 ';

Private $ db;

Public $ dbname;

Private $ table = NULL;

/**

* Initialize the class to obtain the mongo instance object.

*/

Public function _ construct ($ host = NULL, $ port = NULL, $ dbname = NULL, $ table = NULL)

{

If (NULL ===$ dbname ){

$ This-> throwError ('set cannot be blank! ');

}

// Determine whether the host and port are passed

If (NULL! ==$ Host ){

$ This-> host = $ host;

}

If (NULL! ==$ Port ){

$ This-> port = $ port;

}

$ This-> table = $ table;

$ This-> mongo = new MongoClient ($ this-> host. ':'. $ this-> port );

If ($ this-> getVersion ()> = '0. 9.0 '){

$ This-> dbname = $ this-> mongo-> selectDB ($ dbname );

$ This-> db = $ this-> dbname-> selectCollection ($ table );

} Else {

$ This-> db = $ this-> mongo-> $ dbname-> $ table;

}

}

Public function getVersion ()

{

Return response client: VERSION;

}

/**

* Singleton mode

* @ Return Mongo | null

*/

// Public static function getInstance ($ host = null, $ port = null, $ dbname = null, $ table = null ){

//

// If (! (Self: $ instanceof self )){

// Self: $ instanceof = new self ($ host, $ port, $ dbname, $ table );

//}

//

// Return self: $ instanceof;

//}

/**

* Insert a data entry

* @ Param array $ doc

*/

Public function insert ($ doc = array ())

{

If (empty ($ doc )){

$ This-> throwError ('The inserted data cannot be blank! ');

}

// Save data information

Try {

If (! $ This-> db-> insert ($ doc )){

Throw new partition exception ('data insertion failed ');

}

} Catch (except exception $ e ){

$ This-> throwError ($ e-> getMessage ());

}

}

/**

* Insert multiple data records

* @ Param array $ doc

*/

Public function insertMulti ($ doc = array ())

{

If (empty ($ doc )){

$ This-> throwError ('The inserted data cannot be blank! ');

}

// Insert data information

Foreach ($ doc as $ key => $ val ){

// Judge whether $ val is an array.

If (is_array ($ val )){

$ This-> insert ($ val );

}

}

}

/**

* Search for a record

* @ Return array | null

*/

Public function findOne ($ where = NULL)

{

If (NULL ===$ where ){

Try {

If ($ result = $ this-> db-> findOne ()){

Return $ result;

} Else {

Throw new parse exception ('data query failed ');

}

} Catch (except exception $ e ){

$ This-> throwError ($ e-> getMessage ());

}

} Else {

Try {

If ($ result = $ this-> db-> findOne ($ where )){

Return $ result;

} Else {

Throw new parse exception ('data query failed ');

}

} Catch (except exception $ e ){

$ This-> throwError ($ e-> getMessage ());

}

}

}

/**

* Perform subsequent operations with todo conditions

* Search for all documents

* @ Return cursor

*/

Public function find ($ where = NULL)

{

If (NULL ===$ where ){

Try {

If ($ result = $ this-> db-> find ()){

} Else {

Throw new parse exception ('data query failed ');

}

} Catch (except exception $ e ){

$ This-> throwError ($ e-> getMessage ());

}

} Else {

Try {

If ($ result = $ this-> db-> find ($ where )){

} Else {

Throw new parse exception ('data query failed ');

}

} Catch (except exception $ e ){

$ This-> throwError ($ e-> getMessage ());

}

}

$ Arr = array ();

Foreach ($ result as $ id => $ val ){

$ Arr [] = $ val;

}

Return $ arr;

}

/**

* Retrieve the number of records

* @ Return int

*/

Public function getCount ()

{

Try {

If ($ count = $ this-> db-> count ()){

Return $ count;

} Else {

Throw new except exception ('total lookup failed ');

}

} Catch (except exception $ e ){

$ This-> throwError ($ e-> getMessage ());

}

}

/**

* Retrieve all databases

* @ Return array

*/

Public function getDbs ()

{

Return $ this-> mongo-> listDBs ();

}

/**

* Deleting a database

* @ Param null $ dbname

* @ Return mixed

*/

Public function dropDb ($ dbname = NULL)

{

If (NULL! ==$ Dbname ){

$ Retult = $ this-> mongo-> dropDB ($ dbname );

If ($ retult ['OK']) {

Return TRUE;

} Else {

Return FALSE;

}

}

$ This-> throwError ('Enter the name of the database to be deleted ');

}

/**

* Forcibly close the database link

*/

Public function closeDb ()

{

$ This-> mongo-> close (TRUE );

}

/**

* Output error message

* @ Param $ errorInfo error content

*/

Public function throwError ($ errorInfo = '')

{

Echo "Error: $ errorInfo ";

Die ();

}

}

The above is all the content of this article. I hope you will like it.

When talking about php connection to mongoDB, I have to introduce the official php manual at: http://us.php.net/manual/en/book.#.php to share it with everyone...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.