@@ -12,21 +12,35 @@ class MongoDB
12
12
{
13
13
use MongoConnection;
14
14
15
- protected $ db ;
16
- protected $ collection ;
17
- protected $ databaseName ;
18
- protected $ collectionName ;
19
-
15
+ /**
16
+ * Run MongoDB command
17
+ *
18
+ * @return \MongoDB\Driver\Cursor
19
+ */
20
20
public function command (array $ command )
21
21
{
22
22
return static ::getMongoClient ()->{$ this ->admin }->command ($ command );
23
23
}
24
-
24
+ /**
25
+ * Rename collection
26
+ *
27
+ * @param string $fromNs
28
+ * @param string $toNs
29
+ *
30
+ * @return \MongoDB\Driver\Cursor
31
+ */
25
32
public function renameCollection ($ fromNs , $ toNs )
26
33
{
27
34
return $ this ->command (array ('renameCollection ' => $ fromNs , 'to ' => $ toNs ));
28
35
}
29
-
36
+ /**
37
+ * Rename fields
38
+ *
39
+ * @param string $collectionName
40
+ * @param array $fields
41
+ *
42
+ * @return void
43
+ */
30
44
public function renameFields ($ collectionName , $ fields )
31
45
{
32
46
$ rename = [];
@@ -37,24 +51,42 @@ public function renameFields($collectionName, $fields)
37
51
'$rename ' => $ rename ,
38
52
);
39
53
40
- return $ this ->selectCollection ($ collectionName )->updateMany (array (), $ update , array ('upsert ' => true ));
54
+ $ this ->selectCollection ($ collectionName )->updateMany (array (), $ update , array ('upsert ' => true ));
41
55
}
42
-
56
+ /**
57
+ * Get the MongoDB database object.
58
+ *
59
+ * @return \MongoDB\Database
60
+ */
43
61
public function getDB ()
44
62
{
45
63
return DB ::connection ()->getMongoDB ();
46
64
}
47
-
65
+ /**
66
+ * Get the MongoDB collection namespace.
67
+ *
68
+ * @param string $databaseName
69
+ * @param string $collectionName
70
+ * @return string
71
+ */
48
72
public function getNamespace ($ databaseName , $ collectionName )
49
73
{
50
74
return $ databaseName . '. ' . $ collectionName ;
51
75
}
52
-
76
+ /**
77
+ * Get the all collections.
78
+ *
79
+ * @return \MongoDB\Model\CollectionInfoIterator
80
+ */
53
81
public function getCollections ()
54
82
{
55
83
return $ this ->getDB ()->listCollections ();
56
84
}
57
-
85
+ /**
86
+ * Get the all collections name.
87
+ *
88
+ * @return array
89
+ */
58
90
public function getCollectionNames ()
59
91
{
60
92
$ collections = $ this ->getCollections ();
@@ -65,17 +97,35 @@ public function getCollectionNames()
65
97
66
98
return $ collectionNames ;
67
99
}
68
-
100
+ /**
101
+ * Check MongoDB collection
102
+ *
103
+ * @param string $collectionName
104
+ *
105
+ * @return bool
106
+ */
69
107
public function hasCollection ($ collectionName )
70
108
{
71
109
return (in_array ($ collectionName , $ this ->getCollectionNames ())) ? true : false ;
72
110
}
73
-
111
+ /**
112
+ * Create MongoDB colelction
113
+ *
114
+ * @param string $collectionName
115
+ *
116
+ * @return array|object Command result document
117
+ */
74
118
public function createCollection ($ collectionName )
75
119
{
76
120
return $ this ->getDB ()->createCollection ($ collectionName );
77
121
}
78
-
122
+ /**
123
+ * Get MongoDB colelction
124
+ *
125
+ * @param string $collectionName
126
+ *
127
+ * @return array
128
+ */
79
129
public function getCollection ($ collectionName )
80
130
{
81
131
return [
@@ -88,7 +138,13 @@ public function getCollection($collectionName)
88
138
'options ' => [],
89
139
];
90
140
}
91
-
141
+ /**
142
+ * Update MongoDB colelction
143
+ *
144
+ * @param array $collection
145
+ *
146
+ * @return bool
147
+ */
92
148
public function updateCollection ($ collection )
93
149
{
94
150
@@ -111,7 +167,14 @@ public function updateCollection($collection)
111
167
return true ;
112
168
113
169
}
114
-
170
+ /**
171
+ * Rename MongoDB colelction columns
172
+ *
173
+ * @param string $collectionName
174
+ * @param array $fields
175
+ *
176
+ * @return void
177
+ */
115
178
public function renameColumns ($ collectionName , $ fields )
116
179
{
117
180
$ collection = $ this ->selectCollection ($ collectionName );
@@ -139,7 +202,13 @@ public function renameColumns($collectionName, $fields)
139
202
}
140
203
}
141
204
}
142
-
205
+ /**
206
+ * Add MongoDB colelction columns
207
+ *
208
+ * @param string $collectionName
209
+ *
210
+ * @return void
211
+ */
143
212
public function addColumns ($ collectionName )
144
213
{
145
214
$ collection = $ this ->selectCollection ($ collectionName );
@@ -178,7 +247,13 @@ public function addColumns($collectionName)
178
247
179
248
}
180
249
}
181
-
250
+ /**
251
+ * Remove MongoDB colelction columns
252
+ *
253
+ * @param string $collectionName
254
+ *
255
+ * @return void
256
+ */
182
257
public function removeColumns ($ collectionName )
183
258
{
184
259
$ collection = $ this ->selectCollection ($ collectionName );
@@ -198,7 +273,14 @@ public function removeColumns($collectionName)
198
273
$ collection ->updateMany (array (), $ update , array ('upsert ' => true ));
199
274
}
200
275
}
201
-
276
+ /**
277
+ * Set MongoDB colelction fields
278
+ *
279
+ * @param string $collectionName
280
+ * @param array $fields
281
+ *
282
+ * @return void
283
+ */
202
284
public function setFields ($ collectionName , $ fields )
203
285
{
204
286
/*
@@ -216,17 +298,35 @@ public function setFields($collectionName, $fields)
216
298
$ this ->removeColumns ($ collectionName );
217
299
return true ;
218
300
}
219
-
301
+ /**
302
+ * Drop MongoDB colelction
303
+ *
304
+ * @param string $collectionName
305
+ *
306
+ * @return void
307
+ */
220
308
public function dropCollection ($ collectionName )
221
309
{
222
310
$ this ->getDB ()->dropCollection ($ collectionName );
223
311
}
224
-
312
+ /**
313
+ * Select MongoDB colelction
314
+ *
315
+ * @param string $collectionName
316
+ *
317
+ * @return \MongoDB\Collection
318
+ */
225
319
public function selectCollection ($ collectionName )
226
320
{
227
321
return $ this ->getDB ()->selectCollection ($ collectionName );
228
322
}
229
-
323
+ /**
324
+ * Get MongoDB colelction columns
325
+ *
326
+ * @param string $collectionName
327
+ *
328
+ * @return array
329
+ */
230
330
public function getCollectionColumns ($ collectionName )
231
331
{
232
332
$ cursor = $ this ->selectCollection ($ collectionName )->find ();
@@ -241,7 +341,13 @@ public function getCollectionColumns($collectionName)
241
341
242
342
return array_values (array_unique ($ columnNames ));
243
343
}
244
-
344
+ /**
345
+ * Get MongoDB columns
346
+ *
347
+ * @param string $collectionName
348
+ *
349
+ * @return array
350
+ */
245
351
public function getColumns ($ collectionName )
246
352
{
247
353
$ columns = [];
@@ -279,7 +385,13 @@ public function getColumns($collectionName)
279
385
280
386
return $ columns ;
281
387
}
282
-
388
+ /**
389
+ * Get colelction ColumnsName
390
+ *
391
+ * @param string $collectionName
392
+ *
393
+ * @return \Illuminate\Support\Collection
394
+ */
283
395
public function getColumnsName ($ collectionName )
284
396
{
285
397
return DBM_Collection::where ('name ' , $ collectionName )->first ()->fields ->pluck ('name ' )->toArray ();
0 commit comments