我正在尝试模拟以下 MongoDB shellcode:
db.products.find( { $or : [ { title : /blue/i }, { tags : /blue/i } ] }, {_id:0, title:1} );
这是我尝试过的:
bson query[1];
mongo_cursor cursor[1];
bson_init( query );
{
bson_append_start_object( query, "$or");
bson_append_regex( query, "title", "blue", "i" );
bson_append_regex( query, "tags", "blue", "i" );
bson_append_finish_object( query );
}
bson_finish( query );
mongo_cursor_init( cursor, conn, "test.products" );
mongo_cursor_set_query( cursor, query );
while( mongo_cursor_next( cursor ) == MONGO_OK ) {
bson_iterator iterator[1];
if ( bson_find( iterator, mongo_cursor_bson( cursor ), "title" )) {
printf( "%s\n", bson_iterator_string( iterator ) );
}
}
bson_destroy( query );
mongo_cursor_destroy( cursor );
但它似乎没有按预期工作。我还尝试用数组替换对象,然后将数组嵌套在对象中,但无济于事。