0

我正在尝试进行查询,该查询必须从我的 mongodb 数据库中检索一些数据,请按照我的代码:

 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;

我不知道如何使用方法“mongo_find”(第 100 行),它返回一个指向 mongo_cursor 的指针,但我无法操纵这个指针,有一些想法可以帮助我实现这一目标吗?

4

2 回答 2

2

下面是我认为你想要的工作代码。请注意以下事项:

  1. $query 作为运算符或命令不存在,只需使用 BSON 的内部部分
  2. 查询使用“machine.cpu”,因为“cpu”在“machine”的子文档中
  3. 对于字段,我已更改为 bson_append_bool 而不是 bson_append_null 以便您获得以下 bson_find 的结果
  4. 对于您的 bson_find,您必须导航到机器,因为它是包含“cpu”的子文档

希望这能让你前进。

#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;
}

输出

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

bson find cpu: 75
于 2012-05-30T19:44:21.103 回答
0

你的 ptr_cursor 声明在哪里?你可以使用这个:

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));
    }
}
于 2012-05-30T16:14:25.147 回答