这是我收藏的结构部分:
{
...
list: [
{ id:'00A', name:'None 1' },
{ id:'00B', name:'None 2' },
],
...
}您可以建议我使用C lib使用哪种方法来检索"id“和/或"name”字段中的值列表?
发布于 2013-09-14 03:10:46
看起来你是在用C驱动程序要求"db.collection.distinct“的等价物。对吗?如果是这样,您可以使用mongo_run_command函数将distinct作为db命令发出:
http://api.mongodb.org/c/current/api/mongo_8h.html#a155e3de9c71f02600482f10a5805d70d
下面是一段你可能会觉得很有用的代码,演示了这个实现:
mongo conn[1];
int status = mongo_client(conn, "127.0.0.1", 27017);
if (status != MONGO_OK)
return 1;
bson b[1]; // query bson
bson_init(b);
bson_append_string(b, "distinct", "foo");
bson_append_string(b, "key", "list.id"); // or list.name
bson_finish(b);
bson bres[1]; // result bson
status = mongo_run_command(conn, "test", b, bres);
if (status == MONGO_OK){
bson_iterator i[1], sub[1];
bson_type type;
const char* val;
bson_find(i, bres, "values");
bson_iterator_subiterator(i, sub);
while ((type = bson_iterator_next(sub))) {
if (type == BSON_STRING) {
val = bson_iterator_string(sub);
printf("Value: %s\n", val);
}
}
} else {
printf("error: %i\n", status);
}在上面的示例中,数据库是"foo“,包含与您相似的文档的集合是"test”。上面的查询部分相当于:
db.runCommand({distinct:'foo', key:'list.id'})希望这能有所帮助。
杰克
https://stackoverflow.com/questions/18719542
复制相似问题