Frage

I'm trying to make a query that must to retrieve some data from my mongodb database, follow my code:

 72     mongo conn[1];
 73     mongo_cursor cursor[1];
 74 
 75     //Parameters (connection, IP, port);
 76     int status = mongo_connect(conn, "127.0.0.1", 27017);
 77     if(status != MONGO_OK){
 78         switch(conn->err){
 79             case MONGO_CONN_NO_SOCKET: printf("Socket not found\n"); return 1;
 80             case MONGO_CONN_FAIL: printf("Connection Failed\n"); return 1;
 81             case MONGO_CONN_NOT_MASTER: printf("Not master\n"); return 1;
 82         }
 83     }
 84 
 85     //add_new_service(conn, 55, 75,"java","7");
 86     bson query[1];
 87     bson_init(query);
 88     bson_append_start_object(query, "$query");
 89         bson_append_int(query,"cpu",75);
 90     bson_append_finish_object(query);
 91     bson_finish(query);
 92 
 93     bson fields[1];
 94     bson_init(fields);
 95     bson_append_null(fields,"machine");
 96     bson_finish(fields);
 97 
 98     mongo_cursor_init(cursor, conn, "db.services");
 99 
100     cursor = mongo_find(conn,"db.services", query, fields, 9999, 0, 0);
101 
102     while(mongo_cursor_next(ptr_cursor) == MONGO_OK){
103         bson_print(&ptr_cursor->current);
104     }
105 
106     mongo_cursor_destroy(cursor);
107     mongo_destroy(conn);
108     return 0;

I don't know how can i use the method "mongo_find" (line 100), it returns a pointer to a mongo_cursor, but i can't manipulate this pointer, some ideas to help me achieve this?

War es hilfreich?

Lösung

Below is working code that does what I think that you want. Please note the following:

  1. $query as an operator or command does not exist, just use the inner part of your BSON
  2. the query uses "machine.cpu" since "cpu" is in a sub-document of "machine"
  3. for fields, I've changed to bson_append_bool instead of bson_append_null so that you have results for the bson_find below
  4. for your bson_find, you have to navigate into machine since it is a sub-document that contains "cpu"

Hope that this moves you ahead.

#include "mongo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char** argv)
{
    mongo conn[1];
    mongo_cursor* cursor;

    //Parameters (connection, IP, port);
    int status = mongo_connect(conn, "127.0.0.1", 27017);
    if(status != MONGO_OK){
        switch(conn->err){
            case MONGO_CONN_NO_SOCKET: printf("Socket not found\n"); return 1;
            case MONGO_CONN_FAIL: printf("Connection Failed\n"); return 1;
            case MONGO_CONN_NOT_MASTER: printf("Not master\n"); return 1;
        }
    }

    //add_new_service(conn, 55, 75,"java","7");
    bson query[1];
    bson_init(query);
    bson_append_int(query,"machine.cpu",75);
    bson_finish(query);

    bson fields[1];
    bson_init(fields);
    bson_append_bool(fields,"machine",1);
    bson_finish(fields);

    //mongo_cursor_init(cursor, conn, "db.services");

    cursor = mongo_find(conn,"db.services", query, fields, 9999, 0, 0);

    while(mongo_cursor_next(cursor) == MONGO_OK){
        bson_print(mongo_cursor_bson( cursor ));
        bson_iterator it;
        if (bson_find(&it, &cursor->current, "machine")){
            bson sub;
            bson_iterator_subobject( &it, &sub );
            if (bson_find(&it, &sub, "cpu")){
                printf("bson find cpu: %d\n", bson_iterator_int(&it));
            }
        }
    }

    mongo_cursor_destroy(cursor);
    mongo_destroy(conn);
    return 0;
}

output

    _id : 7      4fc51ba2db00e4ba36334713
    machine : 3
        cpu : 16     75
        ram : 16     55

bson find cpu: 75

Andere Tipps

where is your ptr_cursor declaration? you can use this:

while( mongo_cursor_next( &cursor ) ) 
{
    bson_print(cursor.current);

    bson_iterator iter;

    if ( bson_find( &iter, &cursor.current, "***table attribute***" )
    {
        printf("%s", bson_iterator_string(&iter));
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top