1

我有以下 SQL 查询:

SELECT SUM("PAYMENT_HISTORY"."AMOUNT_PAID") FROM "PAYMENT_HISTORY" WHERE (("PAYMENT_HISTORY".YEAR >= 2011) AND ("PAYMENT_HISTORY".YEAR <= 2013) AND ("PAYMENT_HISTORY"."AMOUNT_PAID" >= 500.0 ))

转换后的相同 SQL 从 MongoDB shell 运行并返回值。

db.PAYMENT_HISTORY.aggregate( { $match : {$and: [{YEAR: {$gte : 2011}}, {YEAR: {$lte : 2013}}, {AMOUNT_PAID: {$gte: 500.00}}]}} , { $group : {_id : "POLICY_ID", total:{$sum : "$AMOUNT_PAID"}}} )

当我试图通过 MongoDB C 驱动程序运行时,它不会返回任何结果。

int status = mongo_client( mNoSQLConnection, "127.0.0.1", 27017 );
if (status != MONGO_OK){
    switch (mNoSQLConnection->err) {
        case MONGO_CONN_NO_SOCKET:  printf( "no socket\n" ); 
        case MONGO_CONN_FAIL:       printf( "connection failed\n" ); 
        case MONGO_CONN_NOT_MASTER: printf( "not master\n" ); 
    }
}
else{
    bson query[1], b_result[1];
    mongo_cursor cursor[1];
    bson_init( query );
        bson_append_string(query, "aggregate", "PAYMENT_HISTORY");
            bson_append_start_object(query, "$match");
                bson_append_start_array(query, "$and");
                        bson_append_start_object(query, "$gte");
                            bson_append_int(query, "YEAR", 2011);
                        bson_append_finish_object(query);
                        bson_append_start_object(query, "$lte");
                            bson_append_int(query, "YEAR", 2013);
                        bson_append_finish_object(query);
                        bson_append_start_object(query, "$gte");
                            bson_append_double(query, "AMOUNT_PAID", 500.00);
                        bson_append_finish_object(query);
                bson_append_finish_array(query);
            bson_append_finish_object(query);
            bson_append_start_object(query, "$group");
                bson_append_string(query, "_id", "POLICY_ID");
                bson_append_string(query, "total", "total");
                    bson_append_start_object(query, "$sum");
                        bson_append_start_object(query, "$AMOUNT_PAID");
                        bson_append_finish_object(query);
                    bson_append_finish_object(query);
            bson_append_finish_object(query);
    bson_finish( query );

    bson_print(query);

    int status1;
    status1 = mongo_run_command(mNoSQLConnection, "DB_NAME", query, b_result);
    if (MONGO_OK != status1 )
    {
    }
    else
    {
        /*command results*/
        bson_print(b_result);
    }

    bson_destroy( query );
    mongo_cursor_destroy( cursor );

}

有人可以帮我找出上面代码中的错误吗?

谢谢

4

0 回答 0