Grassroots Infrastructure
The Grassroots Infrastructure is a suite of computing tools to help users and developers use scientific data infrastructure that can easily be interconnected.
mongodb_tool.h
Go to the documentation of this file.
1 /*
2 ** Copyright 2014-2016 The Earlham Institute
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
21 /*
22  * mongodb_tool.h
23  *
24  * Created on: 26 Jun 2015
25  * Author: billy
26  */
27 #ifndef MONGODB_TOOL_H_
28 #define MONGODB_TOOL_H_
29 
30 #include "mongoc.h"
31 #include "typedefs.h"
32 #include "jansson.h"
33 #include "mongodb_library.h"
34 #include "operation.h"
35 
36 
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 
39  #ifdef GRASSROOTS_MONGODB_LIBRARY_EXPORTS // defined if we are building the LIB DLL (instead of using it)
40 
41  #ifdef ALLOCATE_MONGODB_TAGS
42  #define MONGODB_PREFIX GRASSROOTS_MONGODB_API
43  #define MONGODB_VAL(x) = x
44  #else
45  #define MONGODB_PREFIX extern GRASSROOTS_MONGODB_API
46  #define MONGODB_VAL(x)
47  #endif
48  #else
49  #define MONGODB_PREFIX extern GRASSROOTS_MONGODB_API
50  #define MONGODB_VAL(x)
51  #endif
52 
53 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
54 
55 
56 
57 
58 
63 MONGODB_PREFIX const char *MONGO_ID_S MONGODB_VAL("_id");
64 
69 MONGODB_PREFIX const char *MONGO_COLLECTION_S MONGODB_VAL("collection");
70 
76 MONGODB_PREFIX const char *MONGO_OPERATION_S MONGODB_VAL("operation");
77 
83 MONGODB_PREFIX const char *MONGO_OPERATION_INSERT_S MONGODB_VAL("insert");
84 
90 MONGODB_PREFIX const char *MONGO_OPERATION_SEARCH_S MONGODB_VAL("search");
91 
97 MONGODB_PREFIX const char *MONGO_OPERATION_REMOVE_S MONGODB_VAL("remove");
98 
105 MONGODB_PREFIX const char *MONGO_OPERATION_DATA_S MONGODB_VAL("data");
106 
113 MONGODB_PREFIX const char *MONGO_OPERATION_FIELDS_S MONGODB_VAL("fields");
114 
120 MONGODB_PREFIX const char *MONGO_OPERATION_GET_ALL_S MONGODB_VAL("dump");
121 
127 MONGODB_PREFIX const char *MONGO_CLAUSE_OPERATOR_S MONGODB_VAL("operator");
128 
134 MONGODB_PREFIX const char *MONGO_CLAUSE_VALUE_S MONGODB_VAL("value");
135 
136 
137 //MONGODB_PREFIX const uint32 MONGO_OID_STRING_BUFFER_SIZE MONGODB_VAL(25);
138 
139 #define MONGO_OID_STRING_BUFFER_SIZE (25)
140 
141 MONGODB_PREFIX const char *MONGO_OID_KEY_S MONGODB_VAL("$oid");
142 
143 
144 
150 MONGODB_PREFIX const char *MONGO_TIMESTAMP_S MONGODB_VAL ("modified");
151 
152 
159 typedef struct MongoTool
160 {
166  mongoc_client_t *mt_client_p;
167 
168 
170 
176  mongoc_collection_t *mt_collection_p;
177 
178 
184  mongoc_database_t *mt_database_p;
185 
191  mongoc_cursor_t *mt_cursor_p;
192 
193 
194  struct MongoClientManager *mt_manager_p;
195 } MongoTool;
196 
197 
198 /*
199  * The following preprocessor macros allow us to declare
200  * and define the variables in the same place. By default,
201  * they will expand to
202  *
203  * extern const char *SERVICE_NAME_S;
204  *
205  * however if ALLOCATE_MONGODB_TAGS is defined then it will
206  * become
207  *
208  * const char *SERVICE_NAME_S = "path";
209  *
210  * ALLOCATE_MONGODB_TAGS must be defined only once prior to
211  * including this header file. Currently this happens in
212  * mongodb_tool.c.
213  */
214 
215 #ifdef __cplusplus
216 extern "C"
217 {
218 #endif
219 
220 
231 GRASSROOTS_MONGODB_API bool SetMongoToolDatabaseAndCollection (MongoTool *tool_p, const char *db_s, const char *collection_s);
232 
233 
243 GRASSROOTS_MONGODB_API bool SetMongoToolDatabase (MongoTool *tool_p, const char *db_s);
244 
245 
255 GRASSROOTS_MONGODB_API bool SetMongoToolCollection (MongoTool *tool_p, const char *collection_s);
256 
257 
266 GRASSROOTS_MONGODB_API MongoTool *AllocateMongoTool (mongoc_client_t *client_p, struct MongoClientManager *mongo_manager_p);
267 
268 
276 
277 
288 GRASSROOTS_MONGODB_API bson_oid_t *InsertJSONIntoMongoCollection (MongoTool *tool_p, json_t *json_p);
289 
290 
301 GRASSROOTS_MONGODB_API bool UpdateMongoDocumentsByJSON (MongoTool *tool_p, const json_t *query_p, const json_t *update_p, const bool multiple_flag);
302 
303 
315 GRASSROOTS_MONGODB_API bool UpdateMongoDocuments (MongoTool *tool_p, const bson_oid_t *id_p, const json_t *json_p, const bool multiple_flag);
316 
317 
329 GRASSROOTS_MONGODB_API bool RemoveMongoDocuments (MongoTool *tool_p, const json_t *selector_json_p, const bool remove_first_match_only_flag);
330 
331 
345 GRASSROOTS_MONGODB_API bool FindMatchingMongoDocumentsByJSON (MongoTool *tool_p, const json_t *query_json_p, const char **fields_ss, bson_t *extra_opts_p);
346 
347 
361 GRASSROOTS_MONGODB_API bool FindMatchingMongoDocumentsByBSON (MongoTool *tool_p, const bson_t *query_p, const char **fields_ss, bson_t *extra_opts_p);
362 
363 
376 GRASSROOTS_MONGODB_API bool IterateOverMongoResults (MongoTool *tool_p, bool (*process_bson_fn) (const bson_t *document_p, void *data_p), void *data_p);
377 
378 
389 
390 
399 GRASSROOTS_MONGODB_API json_t *GetAllMongoResultsAsJSON (MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p);
400 
401 
411 GRASSROOTS_MONGODB_API bool PopulateJSONWithAllMongoResults (MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p, json_t *results_array_p);
412 
413 
428 GRASSROOTS_MONGODB_API int32 GetAllMongoResultsForKeyValuePair (MongoTool *tool_p, json_t **docs_pp, const char * const key_s, const char * const value_s, const char **fields_ss);
429 
430 
443 GRASSROOTS_MONGODB_API json_t *GetCurrentValuesAsJSON (MongoTool *tool_p, const char **fields_ss, const size_t num_fields);
444 
445 
455 
456 
465 GRASSROOTS_MONGODB_API bool AddBSONDocumentToJSONArray (const bson_t *document_p, void *data_p);
466 
467 
477 GRASSROOTS_MONGODB_API int PrintBSONToLog (const uint32 level, const char *filename_s, const int line_number, const bson_t *bson_p, const char *message_s, ...);
478 
479 
489 GRASSROOTS_MONGODB_API int PrintBSONToErrors (const uint32 level, const char *filename_s, const int line_number, const bson_t *bson_p, const char *message_s, ...);
490 
491 
501 GRASSROOTS_MONGODB_API void LogBSONOid (const bson_oid_t *bson_p, const int level, const char * const filename_s, const int line_number, const char * const prefix_s);
502 
503 
514 GRASSROOTS_MONGODB_API void LogAllBSON (const bson_t *bson_p, const int level, const char * const file_s, const int line, const char * const prefix_s);
515 
516 
528 GRASSROOTS_MONGODB_API int32 IsKeyValuePairInCollection (MongoTool *tool_p, const char *database_s, const char *collection_s, const char *key_s, const char *value_s);
529 
530 
544 GRASSROOTS_MONGODB_API bson_t *GenerateQuery (const json_t *json_p);
545 
546 
554 GRASSROOTS_MONGODB_API json_t *ConvertBSONValueToJSON (const bson_value_t *value_p);
555 
556 
557 
558 
565 GRASSROOTS_MONGODB_API json_t *ConvertBSONToJSON (const bson_t *bson_p);
566 
567 
583 GRASSROOTS_MONGODB_API const char *InsertOrUpdateMongoData (MongoTool *tool_p, json_t *values_p, const char * const database_s, const char * const collection_s, const char **primary_keys_ss, const size_t num_keys, const char * const mapped_id_s, const char * const object_key_s);
584 
585 
599 GRASSROOTS_MONGODB_API const char *EasyInsertOrUpdateMongoData (MongoTool *tool_p, json_t *values_p, const char *const primary_key_id_s);
600 
601 
602 
613 GRASSROOTS_MONGODB_API bool CreateIndexForMongoCollection (MongoTool *tool_p, char **fields_ss);
614 
615 
616 
617 GRASSROOTS_MONGODB_API bool InsertMongoData (MongoTool *tool_p, const json_t *values_p, bson_t **reply_pp, bson_error_t *error_p);
618 
619 
620 GRASSROOTS_MONGODB_API bool InsertMongoDataAsBSON (MongoTool *tool_p, const bson_t *doc_p, bson_t **reply_pp, bson_error_t *error_p);
621 
622 
623 GRASSROOTS_MONGODB_API bool RunMongoCommand (MongoTool *tool_p, bson_t *command_p, bson_t **reply_pp);
624 
625 
626 GRASSROOTS_MONGODB_API bool RemoveMongoDocumentsByBSON (MongoTool *tool_p, const bson_t *selector_p, const bool remove_first_match_only_flag);
627 
628 
629 
636 GRASSROOTS_MONGODB_API bson_t *ConvertJSONToBSON (const json_t *json_p);
637 
638 
639 GRASSROOTS_MONGODB_API char *GetBSONOidAsString (const bson_oid_t *id_p);
640 
641 
642 GRASSROOTS_MONGODB_API void FreeBSONOidString (char *id_s);
643 
644 
645 GRASSROOTS_MONGODB_API bson_oid_t *GetBSONOidFromString (const char *id_s);
646 
647 
648 GRASSROOTS_MONGODB_API bool SaveMongoData (MongoTool *mongo_p, const json_t *data_to_save_p, const char *collection_s, bson_t *selector_p);
649 
650 
651 GRASSROOTS_MONGODB_API bool SaveMongoDataWithTimestamp (MongoTool *mongo_p, json_t *data_to_save_p, const char *collection_s, bson_t *selector_p, const char *timestamp_key_s);
652 
653 GRASSROOTS_MONGODB_API bool SaveAndBackupMongoDataWithTimestamp (MongoTool *mongo_p, json_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p, const char *timestamp_key_s);
654 
655 
656 GRASSROOTS_MONGODB_API bool SaveMongoDataFromBSON (MongoTool *mongo_p, const bson_t *data_to_save_p, const char *collection_s, bson_t *selector_p);
657 
658 
659 GRASSROOTS_MONGODB_API bool SaveAndBackupMongoData (MongoTool *mongo_p, const json_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p);
660 
661 
662 GRASSROOTS_MONGODB_API bool SaveAndBackupMongoDataFromBSON (MongoTool *mongo_p, const bson_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p);
663 
664 
665 GRASSROOTS_MONGODB_API bool SetMongoDataAsBSON (MongoTool *tool_p, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp);
666 
667 
668 GRASSROOTS_MONGODB_API bool UnsetMongoDataAsBSON (MongoTool *tool_p, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp);
669 
670 
671 GRASSROOTS_MONGODB_API bool UpdateMongoDataAsBSON (MongoTool *tool_p, const char * const update_s, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp);
672 
673 
674 GRASSROOTS_MONGODB_API bool SetMongoDataAsBSONForGivenId (MongoTool *tool_p, bson_oid_t *id_p, bson_t *update_p, bson_t **reply_pp);
675 
676 
677 GRASSROOTS_MONGODB_API bool SetMongoData (MongoTool *tool_p, bson_t *selector_p, const json_t *values_p, bson_t **reply_pp);
678 
679 
692 GRASSROOTS_MONGODB_API bool RemoveMongoFields (MongoTool *tool_p, bson_t *selector_p, const char **fields_ss, bson_t **reply_pp);
693 
694 
695 GRASSROOTS_MONGODB_API bson_oid_t *GetNewBSONOid (void);
696 
697 
699 
700 
701 GRASSROOTS_MONGODB_API bson_oid_t *CopyBSONOid (const bson_oid_t *src_p);
702 
703 
704 GRASSROOTS_MONGODB_API void FreeBSONOid (bson_oid_t *id_p);
705 
706 
707 GRASSROOTS_MONGODB_API bson_t *AllocateBSON (void);
708 
709 
710 GRASSROOTS_MONGODB_API void FreeBSON (bson_t *bson_p);
711 
712 
713 GRASSROOTS_MONGODB_API bool GetMongoIdFromJSON (const json_t *data_p, bson_oid_t *id_p);
714 
715 
716 GRASSROOTS_MONGODB_API bool GetNamedIdFromJSON (const json_t *data_p, const char * const key_s, bson_oid_t *id_p);
717 
718 
719 GRASSROOTS_MONGODB_API const char *GetNamedIdAsStringFromJSON (const json_t *data_p, const char * const key_s);
720 
721 
722 GRASSROOTS_MONGODB_API bool GetIdFromJSONKeyValuePair (const json_t *id_val_p, bson_oid_t *id_p);
723 
724 
725 GRASSROOTS_MONGODB_API bool AddCompoundIdToJSON (json_t *data_p, bson_oid_t *id_p);
726 
727 
728 GRASSROOTS_MONGODB_API bool AddNamedCompoundIdToJSON (json_t *data_p, bson_oid_t *id_p, const char *key_s);
729 
730 
731 GRASSROOTS_MONGODB_API bool AddCompoundIdToJSONArray (json_t *array_p, const bson_oid_t *id_p);
732 
733 
734 GRASSROOTS_MONGODB_API bool AddIdToJSON (json_t *data_p, bson_oid_t *id_p, const char *key_s);
735 
736 
737 GRASSROOTS_MONGODB_API bool AddQueryTerm (bson_t *query_p, const char *key_s, const char *value_s, bool regex_flag);
738 
739 
740 GRASSROOTS_MONGODB_API int64 GetNumberOfMongoResults (MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p);
741 
742 
743 GRASSROOTS_MONGODB_API OperationStatus ProcessMongoResults (MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p, bool (*process_bson_fn) (const bson_t *document_p, void *data_p), void *data_p);
744 
745 
746 
757 GRASSROOTS_MONGODB_API bool UpdateMongoDocumentsByBSON (MongoTool *tool_p, const bson_t *query_p, const json_t *update_p, const bool multiple_flag);
758 
759 
760 
774 GRASSROOTS_MONGODB_API bool AddCollectionCompoundIndex (MongoTool *tool_p, const char *database_s, const char * const collection_s, const char ** const keys_ss, const bool unique_flag, const bool sparse_flag);
775 
776 
792 GRASSROOTS_MONGODB_API bool AddCollectionSingleIndex (MongoTool *tool_p, const char *database_s, const char * const collection_s, const char *key_s, const char *index_type_s, const bool unique_flag, const bool sparse_flag);
793 
794 
806 GRASSROOTS_MONGODB_API bool CreateMongoToolCollection (MongoTool *tool_p, const char *collection_s, bson_t *opts_p);
807 
808 
817 GRASSROOTS_MONGODB_API int DoesCollectionExist (MongoTool *tool_p, const char *collection_s);
818 
819 
820 
829 GRASSROOTS_MONGODB_API bool DropCollectionIndex (MongoTool *tool_p, const char *index_s);
830 
831 
832 
833 #ifdef __cplusplus
834 }
835 #endif
836 
837 
838 
839 #endif /* MONGODB_TOOL_H_ */
MongoTool::mt_manager_p
struct MongoClientManager * mt_manager_p
Definition: mongodb_tool.h:194
MongoTool::SetMongoToolDatabase
bool SetMongoToolDatabase(MongoTool *tool_p, const char *db_s)
Set the database that a MongoTool will use.
AddBSONDocumentToJSONArray
bool AddBSONDocumentToJSONArray(const bson_t *document_p, void *data_p)
Convert a BSON document and add it to a JSON array.
MongoTool::CreateIndexForMongoCollection
bool CreateIndexForMongoCollection(MongoTool *tool_p, char **fields_ss)
Create an index for the current collection of a MongoTool.
MongoTool::GetAllExistingMongoResultsAsJSON
json_t * GetAllExistingMongoResultsAsJSON(MongoTool *tool_p)
Convert all of a MongoTool's results into JSON array.
MongoTool::MONGO_COLLECTION_S
const char * MONGO_COLLECTION_S
The identifier used to specify a collection name to use.
Definition: mongodb_tool.h:69
MongoTool::IterateOverMongoResults
bool IterateOverMongoResults(MongoTool *tool_p, bool(*process_bson_fn)(const bson_t *document_p, void *data_p), void *data_p)
Iterate over all of a MongoTool's results.
MongoTool::PopulateJSONWithAllMongoResults
bool PopulateJSONWithAllMongoResults(MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p, json_t *results_array_p)
Fill an existing json array with all the results from searching a mongodb collection.
GetNewUnitialisedBSONOid
bson_oid_t * GetNewUnitialisedBSONOid(void)
MongoTool::UpdateMongoDocumentsByBSON
bool UpdateMongoDocumentsByBSON(MongoTool *tool_p, const bson_t *query_p, const json_t *update_p, const bool multiple_flag)
Update some MongoDB documents.
MongoTool::AddCollectionCompoundIndex
bool AddCollectionCompoundIndex(MongoTool *tool_p, const char *database_s, const char *const collection_s, const char **const keys_ss, const bool unique_flag, const bool sparse_flag)
Create an index for given set of keys.
GetBSONOidAsString
char * GetBSONOidAsString(const bson_oid_t *id_p)
MongoTool::GetCurrentValuesAsJSON
json_t * GetCurrentValuesAsJSON(MongoTool *tool_p, const char **fields_ss, const size_t num_fields)
When iterating over a MongoTool's results get the values and increment the current result that the Mo...
operation.h
GRASSROOTS_MONGODB_API
#define GRASSROOTS_MONGODB_API
Definition: mongodb_library.h:46
MongoTool::IsKeyValuePairInCollection
int32 IsKeyValuePairInCollection(MongoTool *tool_p, const char *database_s, const char *collection_s, const char *key_s, const char *value_s)
Check whether a collection contains any documents with a given key-value pair.
ConvertBSONToJSON
json_t * ConvertBSONToJSON(const bson_t *bson_p)
Create a new JSON fragment from a given BSON one.
MongoTool::SetMongoToolCollection
bool SetMongoToolCollection(MongoTool *tool_p, const char *collection_s)
Set the collection that a MongoTool will use.
SaveAndBackupMongoDataWithTimestamp
bool SaveAndBackupMongoDataWithTimestamp(MongoTool *mongo_p, json_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p, const char *timestamp_key_s)
InsertMongoDataAsBSON
bool InsertMongoDataAsBSON(MongoTool *tool_p, const bson_t *doc_p, bson_t **reply_pp, bson_error_t *error_p)
CopyBSONOid
bson_oid_t * CopyBSONOid(const bson_oid_t *src_p)
AddCompoundIdToJSON
bool AddCompoundIdToJSON(json_t *data_p, bson_oid_t *id_p)
MongoTool::MONGO_OPERATION_REMOVE_S
const char * MONGO_OPERATION_REMOVE_S
The identifier used to specify an remove data from a collection or document.
Definition: mongodb_tool.h:97
MongoTool::MONGO_CLAUSE_OPERATOR_S
const char * MONGO_CLAUSE_OPERATOR_S
The identifier used to specify a query operator.
Definition: mongodb_tool.h:127
GetNamedIdAsStringFromJSON
const char * GetNamedIdAsStringFromJSON(const json_t *data_p, const char *const key_s)
FreeBSONOidString
void FreeBSONOidString(char *id_s)
MongoTool::MONGO_OPERATION_INSERT_S
const char * MONGO_OPERATION_INSERT_S
The identifier used to specify an add data to a collection or document.
Definition: mongodb_tool.h:83
MongoTool::CreateMongoToolCollection
bool CreateMongoToolCollection(MongoTool *tool_p, const char *collection_s, bson_t *opts_p)
Create a Collection.
MongoTool::MONGO_OPERATION_SEARCH_S
const char * MONGO_OPERATION_SEARCH_S
The identifier used to specify searching for documents.
Definition: mongodb_tool.h:90
AddCompoundIdToJSONArray
bool AddCompoundIdToJSONArray(json_t *array_p, const bson_oid_t *id_p)
SaveMongoData
bool SaveMongoData(MongoTool *mongo_p, const json_t *data_to_save_p, const char *collection_s, bson_t *selector_p)
MongoTool
A MongoTool is a datatype that allows access to the data stored within a MongoDB instance.
Definition: mongodb_tool.h:159
MONGO_OID_KEY_S
const char * MONGO_OID_KEY_S
Definition: mongodb_tool.h:141
AddQueryTerm
bool AddQueryTerm(bson_t *query_p, const char *key_s, const char *value_s, bool regex_flag)
GetBSONOidFromString
bson_oid_t * GetBSONOidFromString(const char *id_s)
SetMongoData
bool SetMongoData(MongoTool *tool_p, bson_t *selector_p, const json_t *values_p, bson_t **reply_pp)
typedefs.h
MongoTool::GetAllMongoResultsAsJSON
json_t * GetAllMongoResultsAsJSON(MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p)
Get all results from a mongodb collection.
MONGO_TIMESTAMP_S
const char * MONGO_TIMESTAMP_S
The key for specifying the timestamp of a saved piece of data.
Definition: mongodb_tool.h:150
MongoTool::InsertJSONIntoMongoCollection
bson_oid_t * InsertJSONIntoMongoCollection(MongoTool *tool_p, json_t *json_p)
Insert data from a given JSON fragment using a given MongoTool.
MongoTool::MONGO_CLAUSE_VALUE_S
const char * MONGO_CLAUSE_VALUE_S
The identifier used to specify a query value.
Definition: mongodb_tool.h:134
FreeBSONOid
void FreeBSONOid(bson_oid_t *id_p)
SetMongoDataAsBSONForGivenId
bool SetMongoDataAsBSONForGivenId(MongoTool *tool_p, bson_oid_t *id_p, bson_t *update_p, bson_t **reply_pp)
MongoTool::MONGO_OPERATION_DATA_S
const char * MONGO_OPERATION_DATA_S
The identifier used to specify a JSON fragment denoting operations.
Definition: mongodb_tool.h:105
LogAllBSON
void LogAllBSON(const bson_t *bson_p, const int level, const char *const file_s, const int line, const char *const prefix_s)
Print the JSON representation of a BSON object and all of its children and members to the log Stream.
MongoTool::DoesCollectionExist
int DoesCollectionExist(MongoTool *tool_p, const char *collection_s)
Check if a named Collection exists.
SaveMongoDataWithTimestamp
bool SaveMongoDataWithTimestamp(MongoTool *mongo_p, json_t *data_to_save_p, const char *collection_s, bson_t *selector_p, const char *timestamp_key_s)
AllocateBSON
bson_t * AllocateBSON(void)
MongoTool::FreeMongoTool
void FreeMongoTool(MongoTool *tool_p)
Delete a MongoTool and release the connection that it held.
RunMongoCommand
bool RunMongoCommand(MongoTool *tool_p, bson_t *command_p, bson_t **reply_pp)
MongoTool::RemoveMongoFields
bool RemoveMongoFields(MongoTool *tool_p, bson_t *selector_p, const char **fields_ss, bson_t **reply_pp)
Remove the specified fields from a document.
RemoveMongoDocumentsByBSON
bool RemoveMongoDocumentsByBSON(MongoTool *tool_p, const bson_t *selector_p, const bool remove_first_match_only_flag)
OperationStatus
OperationStatus
The current status of an Operation.
Definition: operation.h:96
MongoTool::MONGO_OPERATION_S
const char * MONGO_OPERATION_S
The identifier used to specify the operation to perform.
Definition: mongodb_tool.h:76
MongoTool::MONGO_OPERATION_GET_ALL_S
const char * MONGO_OPERATION_GET_ALL_S
The identifier used to generate a dump of an entire collection.
Definition: mongodb_tool.h:120
MongoTool::SetMongoToolDatabaseAndCollection
bool SetMongoToolDatabaseAndCollection(MongoTool *tool_p, const char *db_s, const char *collection_s)
Set the database and collection that a MongoTool will use.
UpdateMongoDataAsBSON
bool UpdateMongoDataAsBSON(MongoTool *tool_p, const char *const update_s, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp)
PrintBSONToLog
int PrintBSONToLog(const uint32 level, const char *filename_s, const int line_number, const bson_t *bson_p, const char *message_s,...)
Print the JSON representation of a BSON fragment to the log Stream.
SaveAndBackupMongoData
bool SaveAndBackupMongoData(MongoTool *mongo_p, const json_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p)
GetNamedIdFromJSON
bool GetNamedIdFromJSON(const json_t *data_p, const char *const key_s, bson_oid_t *id_p)
AddIdToJSON
bool AddIdToJSON(json_t *data_p, bson_oid_t *id_p, const char *key_s)
GetNewBSONOid
bson_oid_t * GetNewBSONOid(void)
MongoTool::GetAllMongoResultsForKeyValuePair
int32 GetAllMongoResultsForKeyValuePair(MongoTool *tool_p, json_t **docs_pp, const char *const key_s, const char *const value_s, const char **fields_ss)
Get all results for a given key-value query.
MongoTool::MONGO_ID_S
const char * MONGO_ID_S
The identifier used to uniquely specify a MongoDB document.
Definition: mongodb_tool.h:63
GetIdFromJSONKeyValuePair
bool GetIdFromJSONKeyValuePair(const json_t *id_val_p, bson_oid_t *id_p)
ProcessMongoResults
OperationStatus ProcessMongoResults(MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p, bool(*process_bson_fn)(const bson_t *document_p, void *data_p), void *data_p)
MongoTool::DropCollectionIndex
bool DropCollectionIndex(MongoTool *tool_p, const char *index_s)
Drop an index with a given name from a Collection.
ConvertBSONValueToJSON
json_t * ConvertBSONValueToJSON(const bson_value_t *value_p)
Convert a BSON object into its equivalent JSON type.
MongoTool::UpdateMongoDocumentsByJSON
bool UpdateMongoDocumentsByJSON(MongoTool *tool_p, const json_t *query_p, const json_t *update_p, const bool multiple_flag)
Update some MongoDB documents.
MongoTool::AllocateMongoTool
MongoTool * AllocateMongoTool(mongoc_client_t *client_p, struct MongoClientManager *mongo_manager_p)
This allocates a MongoTool that connects to the MongoDB instance specified in the grassroots....
MongoTool::InsertOrUpdateMongoData
const char * InsertOrUpdateMongoData(MongoTool *tool_p, json_t *values_p, const char *const database_s, const char *const collection_s, const char **primary_keys_ss, const size_t num_keys, const char *const mapped_id_s, const char *const object_key_s)
Insert of update data in a set of matching documents.
GetNumberOfMongoResults
int64 GetNumberOfMongoResults(MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p)
ConvertJSONToBSON
bson_t * ConvertJSONToBSON(const json_t *json_p)
Create a new BSON fragment from a given JSON one.
MongoTool::HasMongoQueryResults
bool HasMongoQueryResults(MongoTool *tool_p)
Check whether a MongoTool has any results after running a query.
SetMongoDataAsBSON
bool SetMongoDataAsBSON(MongoTool *tool_p, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp)
LogBSONOid
void LogBSONOid(const bson_oid_t *bson_p, const int level, const char *const filename_s, const int line_number, const char *const prefix_s)
Print a BSON id to the log Stream.
MongoTool::EasyInsertOrUpdateMongoData
const char * EasyInsertOrUpdateMongoData(MongoTool *tool_p, json_t *values_p, const char *const primary_key_id_s)
Insert of update data in a set of matching documents.
UnsetMongoDataAsBSON
bool UnsetMongoDataAsBSON(MongoTool *tool_p, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp)
MongoTool::FindMatchingMongoDocumentsByBSON
bool FindMatchingMongoDocumentsByBSON(MongoTool *tool_p, const bson_t *query_p, const char **fields_ss, bson_t *extra_opts_p)
Find matching documents for a given query.
PrintBSONToErrors
int PrintBSONToErrors(const uint32 level, const char *filename_s, const int line_number, const bson_t *bson_p, const char *message_s,...)
Print the JSON representation of a BSON fragment to the errors Stream.
GetMongoIdFromJSON
bool GetMongoIdFromJSON(const json_t *data_p, bson_oid_t *id_p)
MongoTool::mt_owns_client_flag
bool mt_owns_client_flag
Definition: mongodb_tool.h:169
MongoTool::UpdateMongoDocuments
bool UpdateMongoDocuments(MongoTool *tool_p, const bson_oid_t *id_p, const json_t *json_p, const bool multiple_flag)
Create a BSON object from the given id and use it to update some MongoDB documents.
SaveMongoDataFromBSON
bool SaveMongoDataFromBSON(MongoTool *mongo_p, const bson_t *data_to_save_p, const char *collection_s, bson_t *selector_p)
FreeBSON
void FreeBSON(bson_t *bson_p)
MongoTool::AddCollectionSingleIndex
bool AddCollectionSingleIndex(MongoTool *tool_p, const char *database_s, const char *const collection_s, const char *key_s, const char *index_type_s, const bool unique_flag, const bool sparse_flag)
Create an index for given key.
MongoTool::FindMatchingMongoDocumentsByJSON
bool FindMatchingMongoDocumentsByJSON(MongoTool *tool_p, const json_t *query_json_p, const char **fields_ss, bson_t *extra_opts_p)
Find matching documents for a given query.
SaveAndBackupMongoDataFromBSON
bool SaveAndBackupMongoDataFromBSON(MongoTool *mongo_p, const bson_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p)
mongodb_library.h
GenerateQuery
bson_t * GenerateQuery(const json_t *json_p)
Create a BSON query from a JSON fragment.
AddNamedCompoundIdToJSON
bool AddNamedCompoundIdToJSON(json_t *data_p, bson_oid_t *id_p, const char *key_s)
MongoTool::MONGO_OPERATION_FIELDS_S
const char * MONGO_OPERATION_FIELDS_S
The identifier used to specify which fields to get the values of for the results of a query.
Definition: mongodb_tool.h:113
InsertMongoData
bool InsertMongoData(MongoTool *tool_p, const json_t *values_p, bson_t **reply_pp, bson_error_t *error_p)
MongoTool::RemoveMongoDocuments
bool RemoveMongoDocuments(MongoTool *tool_p, const json_t *selector_json_p, const bool remove_first_match_only_flag)
Remove some documents from a MongoDB collection.