bson.D与bson.M进行查找查询 [英] bson.D vs bson.M for find queries

查看:399
本文介绍了bson.D与bson.M进行查找查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此特定问题与将gogo软件包 mongo-驱动程序 ,但是我认为这适用于mongodb的大多数接口.

This specifc question is in relation to using mongodb with the golang package mongo-driver, but I would assume this applies across most interfaces with mongodb.

使用 查找 以查询集合中的某些数据,我们可以同时使用 bson.M -和 bson.D 类型为该查找指定过滤器.

When using Find to query some data from a collection, we can use both the bson.M- and bson.D-type to specify the filter for this find.

如果元素的顺序很重要,则应使用文档 bson.D ,否则应使用 bson.M .

As per the documentation bson.D should be used if the order of elements matters and bson.M should be used otherwise.

D是BSON文档的有序表示.当元素的顺序很重要时,例如MongoDB命令文档,应使用此类型.如果元素的顺序无关紧要,则应使用M代替.

D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.

现在我的问题是,使用这两种结构(即有序结构还是无序结构)是否会对mongo查询优化器生成的查询计划产生影响.

Now my question is whether the use of either of these, i.e. ordered vs unordered, structures has an effect on the query plan generated by the mongo query optimizer.

在传统的SQL数据库中,顺序通常无关紧要,因为优化器足够聪明,可以使用摘要统计信息,索引等来确定首先执行哪些查询.

In a classic SQL database the order usually doesn't matter as the optimizer is smart enough to use summary statistics, indexes, etc. to determine which queries to execute first.

在这种情况下,我是否也可以假设这种情况,或者使用有序结构查询我的集合是否会对此产生干扰?使用有序结构的工作方式是否与使用优化程序提示类似?如果存在一些干扰,那么是否对要搜索的字段进行了索引会完全影响到这一点?

Can I assume this to be the case here as well, or does using an ordered structure to query my collection somehow interfere with this / does using an ordered structure work similar to using optimizer hints? If there is some interference, is this influenced at all by if the fields to search through are indexed?

推荐答案

您可以对过滤器使用 bson.M ,它通常会导致过滤器声明更短,更清晰,字段的顺序不正确.无论如何,无论使用什么顺序,MongoDB服务器都足够智能以找到匹配的索引.例如.如果您的复合索引的字段为 A B ,请先使用 bson.D 过滤器列出 B A 不会阻止服务器使用现有索引.因此,在这种情况下,您可以使用 bson.M bson.D ,没关系.

You may use bson.M for the filter, it usually results in shorter and clearer filter declaration, the order of fields doesn't matter, the MongoDB server is smart enough to find matching indices regardless of the used order. E.g. if you have a compound index with fields A and B, using a bson.D filter listing B first then A will not prevent the server to use the existing index. So in this case you may use bson.M and bson.D, it doesn't matter.

例如,当您指定排序字段时,顺序就很重要.如果先按字段 A 然后按字段 B 进行排序,则与先按 B 然后再按
排序的顺序可能完全不同. A .因此,当您指定具有多个字段的排序文档时,一定要使用 bson.D .

The order does matter when you specify sort fields for example. It does matter if you sort by field A then by field B, it may be a completely different order than sorting by B first and then by A. So when you specify a sort document having multiple fields, you should definitely use bson.D.

例如,在插入新文档时,顺序也可能很重要.如果使用 bson.M 作为文档,则不能保证所有文档中的字段顺序都相同.当您使用 bson.D 时,保存的文档中的顺序将与您在 bson.D 中列出的字段的顺序匹配.

The order may also matter when you insert a new document for example. If you use a bson.M as the document, the order of fields is not guaranteed to be the same in all your documents. When you use bson.D, then the order in the saved document will match the order as you list the fields in bson.D.

这篇关于bson.D与bson.M进行查找查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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