【发布时间】:2012-05-04 16:27:15
【问题描述】:
我正在尝试模拟以下 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 );
但它似乎没有按预期工作。我也尝试用数组替换对象,然后将数组嵌套在对象中,但无济于事。
【问题讨论】:
-
$or 需要一个对象数组。您能否编辑您的问题以显示您如何尝试使用对象数组?请记住,有 3 个级别:顶级查询、$or 的数组字段以及数组内每个子句的嵌套对象。另外,请在您的查询中发布 bson_print() 的输出,以便于调试。
标签: c mongodb mongodb-query mongodb-c