简体   繁体   English

mongo c 驱动程序中 bson_iter_find 的顺序是否重要

[英]Does order matter in bson_iter_find in mongo c driver

I am using mongo c driver 1.1 with mongo version 3.0.我正在使用 mongo c 驱动程序 1.1 和 mongo 3.0 版。 Libbson version 1.1.利布森 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.以下代码仅在 mongodb 中“fieldA”高于“fieldB”时有效。 If i change the order bson_iter_find returns false.如果我更改顺序 bson_iter_find 返回 false。

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

In older versions of the libbson(0.4) I was able to use bson_find(), to look for fields in a doc.在旧版本的 libbson(0.4) 中,我能够使用 bson_find() 来查找文档中的字段。 Is there something similar i can use in the new libbson library?我可以在新的 libbson 库中使用类似的东西吗?

Link to new libbson library https://api.mongodb.org/libbson/current/链接到新的 libbson 库https://api.mongodb.org/libbson/current/

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_iter_init ( http://api.mongodb.org/libbson/current/bson_iter_init.html )。

Presumably you have a single bson_iter_init call on a bson_t object.大概你在 bson_t 对象上有一个 bson_iter_init 调用。 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.

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_init_find (http://api.mongodb.org/libbson/current/bson_iter_init_find.html )。

   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.

If you're interested in why, I work on the bsonsearch ( https://github.com/bauman/bsonsearch ) library and have similar problems.如果您对原因感兴趣,我在 bsonsearch ( https://github.com/bauman/bsonsearch ) 库上工作并遇到类似的问题。

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.在 libbson 中,几乎所有幕后操作都在操作指向内存区域的指针。

The reason ordering maters is because you initialized once, when you called iter_find, libbson would seek past B to locate A .排序 maters 的原因是因为你初始化了一次,当你调用 iter_find 时,libbson 会寻找经过 B 来定位 A The subsequent call to find B would seek to the end of the buffer and miss it.对 find B 的后续调用将寻找缓冲区的末尾并错过它。 You avoid that problem by reinitializing the iterator back to position 0 and start the seek from there.您可以通过将迭代器重新初始化回位置 0 并从那里开始查找来避免该问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM