static void
test_mongoc_client_command_secondary (void)
{
    mongoc_client_t *client;
    mongoc_cursor_t *cursor;
    mongoc_read_prefs_t *read_prefs;
    bson_t cmd = BSON_INITIALIZER;

    client = mongoc_client_new (gTestUri);
    assert (client);

    BSON_APPEND_INT32 (&cmd, "invalid_command_here", 1);

    read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY_PREFERRED);

    suppress_one_message ();
    cursor = mongoc_client_command (client, "admin", MONGOC_QUERY_NONE, 0, 1, 0, &cmd, NULL, read_prefs);

    mongoc_read_prefs_destroy (read_prefs);

    /* ensure we detected this must go to primary */
    assert (cursor->redir_primary);

    mongoc_cursor_destroy (cursor);
    mongoc_client_destroy (client);
    bson_destroy (&cmd);
}
static void
test_mongoc_client_command (void)
{
    mongoc_client_t *client;
    mongoc_cursor_t *cursor;
    const bson_t *doc;
    bool r;
    bson_t cmd = BSON_INITIALIZER;

    client = mongoc_client_new (gTestUri);
    assert (client);

    bson_append_int32 (&cmd, "ping", 4, 1);

    cursor = mongoc_client_command (client, "admin", MONGOC_QUERY_NONE, 0, 1, 0, &cmd, NULL, NULL);
    assert (!cursor->redir_primary);

    r = mongoc_cursor_next (cursor, &doc);
    assert (r);
    assert (doc);

    r = mongoc_cursor_next (cursor, &doc);
    assert (!r);
    assert (!doc);

    mongoc_cursor_destroy (cursor);
    mongoc_client_destroy (client);
    bson_destroy (&cmd);
}
Ejemplo n.º 3
0
bool
_mongoc_client_command_simple_with_hint (mongoc_client_t           *client,
                                         const char                *db_name,
                                         const bson_t              *command,
                                         const mongoc_read_prefs_t *read_prefs,
                                         bool                       is_write_command,
                                         bson_t                    *reply,
                                         uint32_t                   hint,
                                         bson_error_t              *error)
{
   mongoc_cursor_t *cursor;
   const bson_t *doc;
   bool ret;

   BSON_ASSERT (client);
   BSON_ASSERT (db_name);
   BSON_ASSERT (command);

   cursor = mongoc_client_command (client, db_name, MONGOC_QUERY_NONE, 0, 1, 0,
                                   command, NULL, read_prefs);

   cursor->hint = hint;
   cursor->is_write_command = is_write_command ? 1 : 0;

   ret = mongoc_cursor_next (cursor, &doc);

   if (reply) {
      if (ret) {
         bson_copy_to (doc, reply);
      } else {
         bson_init (reply);
      }
   }

   if (!ret) {
      mongoc_cursor_error (cursor, error);
   }

   mongoc_cursor_destroy (cursor);

   return ret;
}
Ejemplo n.º 4
0
mongoc_cursor_t *
mongoc_database_command (mongoc_database_t         *database,
                         mongoc_query_flags_t       flags,
                         uint32_t              skip,
                         uint32_t              limit,
                         uint32_t              batch_size,
                         const bson_t              *command,
                         const bson_t              *fields,
                         const mongoc_read_prefs_t *read_prefs)
{
   BSON_ASSERT (database);
   BSON_ASSERT (command);

   if (!read_prefs) {
      read_prefs = database->read_prefs;
   }

   return mongoc_client_command (database->client, database->name, flags, skip,
                                 limit, batch_size, command, fields, read_prefs);
}
Ejemplo n.º 5
0
mongoc_cursor_t *
mongoc_client_find_databases (mongoc_client_t *client,
                              bson_error_t    *error)
{
   bson_t cmd = BSON_INITIALIZER;
   mongoc_cursor_t *cursor;

   BSON_ASSERT (client);

   BSON_APPEND_INT32 (&cmd, "listDatabases", 1);

   cursor = mongoc_client_command (client, "admin", MONGOC_QUERY_SLAVE_OK, 0, 0, 0,
                                   &cmd, NULL, NULL);

   _mongoc_cursor_array_init(cursor, "databases");

   cursor->limit = 0;

   bson_destroy (&cmd);

   return cursor;
}
Ejemplo n.º 6
0
mongoc_cursor_t *
mongoc_database_command (mongoc_database_t         *database,
                         mongoc_query_flags_t       flags,
                         uint32_t                   skip,
                         uint32_t                   limit,
                         uint32_t                   batch_size,
                         const bson_t              *command,
                         const bson_t              *fields,
                         const mongoc_read_prefs_t *read_prefs)
{
   BSON_ASSERT (database);
   BSON_ASSERT (command);

   /* Server Selection Spec: "The generic command method has a default read
    * preference of mode 'primary'. The generic command method MUST ignore any
    * default read preference from client, database or collection
    * configuration. The generic command method SHOULD allow an optional read
    * preference argument."
    */
   return mongoc_client_command (database->client, database->name, flags, skip,
                                 limit, batch_size, command, fields, read_prefs);
}
Ejemplo n.º 7
0
/*
 * Count the number of documents.
 */
double
MongoAggregateCount(MONGO_CONN* conn, const char* database, const char* collection, const BSON* b)
{
	BSON            *command = NULL;
	BSON            *reply = NULL;
	BSON            *doc = NULL;
	double          count = 0;
	mongoc_cursor_t *cursor = NULL;
	bool            ret = false;

	command = BsonCreate();
	reply = BsonCreate();
	BsonAppendUTF8(command, "count", (char*)collection);
	if (b) /* not empty */
		BsonAppendBson(command, "query", (BSON*)b);

	BsonFinish(command);

	cursor = mongoc_client_command(conn, database, MONGOC_QUERY_SLAVE_OK, 0, 1, 0, command, NULL, NULL);
	if (cursor)
	{
		ret = mongoc_cursor_next(cursor, (const BSON**)&doc);
		if (ret)
		{
			bson_iter_t it;
			bson_copy_to(doc, reply);
			if (bson_iter_init_find(&it, reply, "n"))
				count = BsonIterDouble(&it);
			BsonDestroy(doc);
		}
		mongoc_cursor_destroy(cursor);
	}
	BsonDestroy(reply);
	BsonDestroy(command);
	return count;
}