gpt4 book ai didi

查询中的 mongodb c-driver $orderby

转载 作者:可可西里 更新时间:2023-11-01 09:52:57 24 4
gpt4 key购买 nike

我正在尝试使用 sort ($orderby) 关键字对 mongo 集合进行查询。

这里是没有 $orderby 的 bson 查询对象:

bson cmd;
bson_init( &cmd );
bson_append_string( &cmd, "ip", ip.c_str() );
bson_append_start_object( &cmd, "timestamp" );
bson_append_long( &cmd, "$gt", 100 );
bson_append_finish_object( &cmd );
bson_finish( &cmd );

mongo_find( conn, collection, cmd, bson_empty( &empty ), 0, 0, MONGO_SLAVE_OK );

效果很好,返回了 5000 个结果。

当我添加 $orderby 对象时(就像在此处的 c-driver 示例中 http://api.mongodb.org/c/current/tutorial.html#complex-queries ):

bson cmd;
bson_init( &cmd );
bson_append_string( &cmd, "ip", ip.c_str() );
bson_append_start_object( &cmd, "timestamp" );
bson_append_long( &cmd, "$gt", 100 );
bson_append_finish_object( &cmd );

bson_append_start_object( &cmd, "$orderby" );
bson_append_int( &cmd, "timestamp", 1 );
bson_append_finish_object( &cmd );

bson_finish( &cmd );
...

我有 0 个结果。

我做错了什么?

最佳答案

尝试:

bson cmd;
bson_init( &cmd );

bson_append_start_object( &cmd, "$query" );
bson_append_string( &cmd, "ip", ip.c_str() );
bson_append_start_object( &cmd, "timestamp" );
bson_append_long( &cmd, "$gt", 100 );
bson_append_finish_object( &cmd );
bson_append_finish_object( &cmd );

bson_append_start_object( &cmd, "$orderby" );
bson_append_int( &cmd, "timestamp", 1 );
bson_append_finish_object( &cmd );

bson_finish( &cmd );

关于查询中的 mongodb c-driver $orderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12317440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com