@@ -1105,3 +1105,66 @@ mongoc_client_command (mongoc_client_t *client,
1105
1105
return _mongoc_cursor_new (client , ns , flags , skip , n_return , 100 , TRUE,
1106
1106
query , fields , read_prefs );
1107
1107
}
1108
+
1109
+
1110
+ /**
1111
+ * mongoc_client_command_simple:
1112
+ * @client: A mongoc_client_t.
1113
+ * @db_name: The namespace, such as "admin".
1114
+ * @command: The command to execute.
1115
+ * @read_prefs: The read preferences or NULL.
1116
+ * @reply: A location for the reply document or NULL.
1117
+ * @error: A location for the error, or NULL.
1118
+ *
1119
+ * This wrapper around mongoc_client_command() aims to make it simpler to
1120
+ * run a command and check the output result.
1121
+ *
1122
+ * FALSE is returned if the command failed to be delivered or if the execution
1123
+ * of the command failed. For example, a command that returns {'ok': 0} will
1124
+ * result in this function returning FALSE.
1125
+ *
1126
+ * To allow the caller to disambiguate between command execution failure and
1127
+ * failure to send the command, reply will always be set if non-NULL. The
1128
+ * caller should release this with bson_destroy().
1129
+ *
1130
+ * Returns: TRUE if the command executed and resulted in success. Otherwise
1131
+ * FALSE and @error is set. @reply is always set, either to the resulting
1132
+ * document or an empty bson document upon failure.
1133
+ */
1134
+ bson_bool_t
1135
+ mongoc_client_command_simple (mongoc_client_t * client ,
1136
+ const char * db_name ,
1137
+ const bson_t * command ,
1138
+ const mongoc_read_prefs_t * read_prefs ,
1139
+ bson_t * reply ,
1140
+ bson_error_t * error )
1141
+ {
1142
+ mongoc_cursor_t * cursor ;
1143
+ const bson_t * doc ;
1144
+ bson_bool_t ret ;
1145
+
1146
+ BSON_ASSERT (client );
1147
+ BSON_ASSERT (db_name );
1148
+ BSON_ASSERT (command );
1149
+
1150
+ cursor = mongoc_client_command (client , db_name , MONGOC_QUERY_NONE , 0 , 1 ,
1151
+ command , NULL , read_prefs );
1152
+
1153
+ ret = mongoc_cursor_next (cursor , & doc );
1154
+
1155
+ if (reply ) {
1156
+ if (ret ) {
1157
+ bson_copy_to (doc , reply );
1158
+ } else {
1159
+ bson_init (reply );
1160
+ }
1161
+ }
1162
+
1163
+ if (!ret ) {
1164
+ mongoc_cursor_error (cursor , error );
1165
+ }
1166
+
1167
+ mongoc_cursor_destroy (cursor );
1168
+
1169
+ return ret ;
1170
+ }
0 commit comments