在Mongo C驱动程序的bson_iter_find中顺序是否重要 [英] Does order matter in bson_iter_find in mongo c driver

查看:188
本文介绍了在Mongo C驱动程序的bson_iter_find中顺序是否重要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongo 3.0版中使用mongo c驱动程序1.1. Libbson版本1.1. 我正在使用迭代器在文档中查找某些字段.以下代码仅在mongodb中"fieldA"高于"fieldB"时才有效.如果我更改顺序bson_iter_find返回false.

I am using mongo c driver 1.1 with mongo version 3.0. Libbson version 1.1. I am using an iterator to look for certain fields in a document. The following code only works when "fieldA" is above "fieldB" in mongodb. If i change the order bson_iter_find returns false.

if(bson_iter_find(&iterator,"fieldA")){
    pintf("fieldA");
}
if(bson_iter_find(&iterator,"fieldB")){
    pintf("fieldB");
}

在旧版本的libbson(0.4)中,我能够使用bson_find()查找文档中的字段.我可以在新的libbson库中使用类似的东西吗?

In older versions of the libbson(0.4) I was able to use bson_find(), to look for fields in a doc. Is there something similar i can use in the new libbson library?

链接到新的libbson库 https://api.mongodb.org/libbson/current/

Link to new libbson library https://api.mongodb.org/libbson/current/

推荐答案

要直接回答您的问题,您应该致电bson_iter_init( http://api.mongodb.org/libbson/current/bson_iter_init.html ).

to directly answer your question, you should call bson_iter_init (http://api.mongodb.org/libbson/current/bson_iter_init.html) for every single "new" query you're making against the data.

大概您对bson_t对象有一个bson_iter_init调用.您只需要另一个.

Presumably you have a single bson_iter_init call on a bson_t object. You just need another.

   bson_iter_t iterator1;
   bson_iter_t iterator2;

   if (bson_iter_init (&iterator1, doc) &&
       bson_iter_find (&iterator1, "fieldA") ) {
       //Do something with fieldA
    }

   if (bson_iter_init (&iterator2, doc) &&
       bson_iter_find (&iterator2, "fieldB") ) {
       //Do something with fieldB
    }
    bson_free(doc); //DONT FORGET TO DESTROY THE BSON_T object at the end.

或者,只需使用组合命令bson_iter_init_find( http://api.mongodb.org /libbson/current/bson_iter_init_find.html ),如果您不想处理内部问题.

or, just use the combined command bson_iter_init_find (http://api.mongodb.org/libbson/current/bson_iter_init_find.html) if you don't want to deal with the internals.

   bson_iter_t iterator1;
   bson_iter_t iterator2;

   if (bson_iter_init_find (&iterator1, doc, "fieldA") ) {
       //Do something with fieldA
    }

   if (bson_iter_init_find (&iterator2, doc,"fieldB") ) {
       //Do something with fieldB
    }
    bson_free(doc); //DONT FORGET TO DESTROY THE BSON_T object at the end.

如果您对为什么感兴趣,我会从事bsonsearch的工作( https://github.com/bauman/bsonsearch )库,并且有类似的问题.

If you're interested in why, I work on the bsonsearch (https://github.com/bauman/bsonsearch) library and have similar problems.

在处理指针时要非常谨慎. libbson中的几乎所有东西都在操纵指向内存中某个区域的指针.

Be very cautious on how you're dealing with pointers. Nearly everything under the hood in libbson is manipulating pointers to an area in memory.

排序的原因是因为您初始化了一次,所以当您调用iter_find时,libbson会越过B来找到A .随后的查找B的调用将寻求到缓冲区的末尾并错过它.通过将迭代器重新初始化回位置0并从此处开始查找,可以避免该问题.

The reason ordering maters is because you initialized once, when you called iter_find, libbson would seek past B to locate A. The subsequent call to find B would seek to the end of the buffer and miss it. You avoid that problem by reinitializing the iterator back to position 0 and start the seek from there.

除非您确切地知道自己在做什么,并且想要优化缓冲区周围的查找,否则最好只为每个查找重新初始化迭代器.

Unless you know exactly what you're doing and want to optimize seeks around the buffer, it's probably best to just reinitialize the iterator for every find.

这篇关于在Mongo C驱动程序的bson_iter_find中顺序是否重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆