Skip to content

Commit 2cc905a

Browse files
Mark SpritzlerNorman Maurer
Mark Spritzler
authored and
Norman Maurer
committed
Add back dropCollection to the Persistor.
Add documentation for count, getCollections, DropCollection, and CollectionStats. Fixed a couple of typos. Added tests for drop collections combined with getCollections, and also a test for CollectionStats
1 parent e1f82ec commit 2cc905a

File tree

4 files changed

+344
-35
lines changed

4 files changed

+344
-35
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.idea
55
.project
66
.settings
7+
*.iml
78
module.iml
89
bin
910
build

README.md

+209-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# MongoDB Persistor
22

3-
This module allows data to be saved, retrieved, searched for, and deleted in a MongoDB instance. MongoDB is a great match for persisting vert.x data since it natively handles JSON (BSON) documents. To use this module you must have a MongoDB instance running on your network.
3+
This module allows data to be saved, retrieved, searched for, and deleted in a MongoDB instance. MongoDB is a great match
4+
for persisting vert.x data since it natively handles JSON (BSON) documents.
5+
6+
####To use this module you must have a MongoDB instance running on your network.
47

58
This is a multi-threaded worker module.
69

@@ -84,7 +87,7 @@ When the save complete successfully, a reply message is sent back to the sender
8487
"status": "ok"
8588
}
8689

87-
The reply will also contain a field `_id` if the document that was saved didn't specifiy an id, this will be an automatically generated UUID, for example:
90+
The reply will also contain a field `_id` if the document that was saved didn't specify an id, this will be an automatically generated UUID, for example:
8891

8992
{
9093
"status": "ok"
@@ -100,7 +103,8 @@ If an error occurs in saving the document a reply is returned:
100103
"message": <message>
101104
}
102105

103-
Where `message` is an error message.
106+
Where
107+
* `message` is an error message.
104108

105109

106110
### Update
@@ -168,7 +172,7 @@ To find documents send a JSON message to the module main address:
168172

169173
Where:
170174
* `collection` is the name of the MongoDB collection that you wish to search in in. This field is mandatory.
171-
* `matcher` is a JSON object that you want to match against to find matching documents. This obeys the normal MongoDB matching rues.
175+
* `matcher` is a JSON object that you want to match against to find matching documents. This obeys the normal MongoDB matching rules.
172176
* `sort_query` provides an order for sorting the responses that you are returned.
173177
* `keys` is an optional JSON object that contains the fields that should be returned for matched documents. See MongoDB manual for more information. Example: { "name": 1 } will only return objects with _id and the name field
174178
* `skip` is a number which determines the number of documents to skip. This is optional. By default no documents are skipped.
@@ -194,7 +198,8 @@ When the find complete successfully, a reply message is sent back to the sender
194198
"results": <results>
195199
}
196200

197-
Where `results` is a JSON array containing the results of the find operation. For example:
201+
Where
202+
*`results` is a JSON array containing the results of the find operation. For example:
198203

199204
{
200205
"status": "ok",
@@ -224,7 +229,8 @@ If an error occurs in finding the documents a reply is returned:
224229
"message": <message>
225230
}
226231

227-
Where `message` is an error message.
232+
Where
233+
*`message` is an error message.
228234

229235
If you would like to paginate your result :
230236

@@ -243,26 +249,6 @@ Equivalence in mongoDB:
243249

244250
db.order.find().skip(10).limit(10)
245251

246-
### Count
247-
248-
Count all matching document in the database.
249-
250-
To count a document send a JSON message to the module main address:
251-
252-
{
253-
"action": "count",
254-
"collection": <collection>,
255-
"matcher": <matcher>
256-
}
257-
258-
When the count complete successfully, a reply message is sent back to the sender with the following data:
259-
260-
{
261-
"status": "ok",
262-
"count" : <number>
263-
}
264-
265-
266252
#### Batching
267253

268254
If a find returns many documents we do not want to load them all up into memory at once and send them in a single JSON message since this could result in the server running out of RAM.
@@ -315,7 +301,7 @@ To find a document send a JSON message to the module main address:
315301

316302
Where:
317303
* `collection` is the name of the MongoDB collection that you wish to search in in. This field is mandatory.
318-
* `matcher` is a JSON object that you want to match against to find a matching document. This obeys the normal MongoDB matching rues.
304+
* `matcher` is a JSON object that you want to match against to find a matching document. This obeys the normal MongoDB matching rules.
319305
* `keys` is an optional JSON object that contains the fields that should be returned for matched documents. See MongoDB manual for more information. Example: { "name": 1 } will only return objects with _id and the name field
320306

321307
If more than one document matches, just the first one will be returned.
@@ -346,7 +332,56 @@ If an error occurs in finding the documents a reply is returned:
346332
"message": <message>
347333
}
348334

349-
Where `message` is an error message.
335+
Where
336+
*`message` is an error message.
337+
338+
### Count
339+
340+
Counts the number of documents within a collection:
341+
342+
{
343+
"action": "count",
344+
"collection": <collection>,
345+
"matcher": <matcher>
346+
}
347+
348+
Where:
349+
* `collection` is the name of the MongoDB collection that you wish to delete from. This field is mandatory.
350+
* `matcher` is a JSON object that you want to match against to count matching documents. This obeys the normal MongoDB matching rules.
351+
352+
All documents within the collection will be counted.
353+
354+
An example would be:
355+
356+
{
357+
"action": "count",
358+
"collection": "items",
359+
"matcher": {
360+
"active": true
361+
}
362+
}
363+
364+
This should return the count of all documents that have the attribute active set to true.
365+
366+
When the count completes successfully, a reply message is sent back to the sender with the following data:
367+
368+
{
369+
"status": "ok",
370+
"count": <count>
371+
}
372+
373+
Where
374+
*`count` is the number of documents in the collection that matched the matcher.
375+
376+
If an error occurs in finding the documents a reply is returned:
377+
378+
{
379+
"status": "error",
380+
"message": <message>
381+
}
382+
383+
Where
384+
*`message` is an error message.
350385

351386
### Delete
352387

@@ -362,7 +397,7 @@ To delete documents send a JSON message to the module main address:
362397

363398
Where:
364399
* `collection` is the name of the MongoDB collection that you wish to delete from. This field is mandatory.
365-
* `matcher` is a JSON object that you want to match against to delete matching documents. This obeys the normal MongoDB matching rues.
400+
* `matcher` is a JSON object that you want to match against to delete matching documents. This obeys the normal MongoDB matching rules.
366401

367402
All documents that match will be deleted.
368403

@@ -385,7 +420,8 @@ When the find complete successfully, a reply message is sent back to the sender
385420
"number": <number>
386421
}
387422

388-
Where `number` is the number of documents deleted.
423+
Where
424+
*`number` is the number of documents deleted.
389425

390426
If an error occurs in finding the documents a reply is returned:
391427

@@ -394,7 +430,148 @@ If an error occurs in finding the documents a reply is returned:
394430
"message": <message>
395431
}
396432

397-
Where `message` is an error message.
433+
Where
434+
*`message` is an error message.
435+
436+
### Get Collections List
437+
438+
Returns the list of collection names in the db:
439+
440+
{
441+
"action": "getCollections"
442+
}
443+
444+
All collections within the current db will be returned if they exist.
445+
446+
An example would be:
447+
448+
{
449+
"action": "getCollections"
450+
}
451+
452+
This should return the list of all collections within the db
453+
454+
When getCollections completes successfully, a reply message is sent back to the sender with the following data:
455+
456+
{
457+
"status": "ok",
458+
"collections": [
459+
<listOfCollections>
460+
]
461+
}
462+
463+
Where
464+
* <listOfCollections> is a list containing each collection name in the db.
465+
466+
If an error occurs in finding the documents a reply is returned:
467+
468+
{
469+
"status": "error",
470+
"message": <message>
471+
}
472+
473+
Where
474+
* `message` is an error message.
475+
476+
### DB stats
477+
478+
Returns statistics about the db:
479+
480+
{
481+
"action": "collectionStats",
482+
"collection": <collection>
483+
}
484+
485+
Where:
486+
* `collection` is the name of the MongoDB collection that you wish to get statistics on in the db. This field is mandatory.
487+
488+
An example would be:
489+
490+
{
491+
"action": "collectionStats",
492+
"collection": "items"
493+
}
494+
495+
This will return the statistics for the items collection within the db.
496+
497+
When collectionStats completes successfully, a reply message is sent back to the sender with the following data:
498+
499+
{
500+
"status": "ok",
501+
"stats": {
502+
"serverUsed":"localhost/127.0.0.1:27017",
503+
"ns"": "test_coll.items",
504+
"count": 1,
505+
"size": 136,
506+
"avgObjSize": 136.0,
507+
"storageSize": 8192,
508+
"numExtents": 1,
509+
"nindexes": 1,
510+
"lastExtentSize": 8192,
511+
"paddingFactor": 1.0,
512+
"systemFlags": 1,
513+
"userFlags": 0,
514+
"totalIndexSize": 8176,
515+
"indexSizes": {
516+
"_id_":8176
517+
},
518+
"ok":1.0
519+
}
520+
}
521+
522+
Instead of putting placeholders in the DOC here are almost all the real values from the test_client.js run which tests collectionStats
523+
I did change the "ns" value to match the collection name in the sample above.
524+
525+
If an error occurs in finding the documents a reply is returned:
526+
527+
{
528+
"status": "error",
529+
"message": <message>
530+
}
531+
532+
Where
533+
* `message` is an error message.
534+
535+
### Drop Collection
536+
537+
Drops a collection from the db:
538+
539+
{
540+
"action": "dropCollection",
541+
"collection": <collection>
542+
}
543+
544+
Where:
545+
* `collection` is the name of the MongoDB collection that you wish to drop from the db. This field is mandatory.
546+
547+
The collection will be removed from the db. This means that all the documents within the collection are gone. Use with CARE
548+
549+
An example would be:
550+
551+
{
552+
"action": "dropCollection",
553+
"collection": "items"
554+
}
555+
556+
This should return an "ok" response, but nothing else. Check out the test_client.js to see our test to make sure
557+
that dropCollection works. We just then go retrieve the collection list to make sure the collection we dropped is not
558+
in the collection list returned but "getCollections" action
559+
560+
When the drop completes successfully, a reply message is sent back to the sender with the following data:
561+
562+
{
563+
"status": "ok",
564+
}
565+
566+
If an error occurs in finding the documents a reply is returned:
567+
568+
{
569+
"status": "error",
570+
"message": <message>
571+
}
572+
573+
Where
574+
* `message` is an error message.
398575

399576
### Command
400577

@@ -446,5 +623,5 @@ An example for delete would be
446623
"action": "delete",
447624
"collection": <collection>,
448625
"matcher": <matcher>,
449-
"write_concern": "SAFE"
626+
"writeConcern": "SAFE"
450627
}

0 commit comments

Comments
 (0)