在mongoose中使用mongodb多键索引方法为引用建立索引数组 [英] Indexing array of references using mongodb multikey indexing method in mongoose

查看:51
本文介绍了在mongoose中使用mongodb多键索引方法为引用建立索引数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模式有一个ObjectId数组,这些对象是对另一个模式的引用.我想要的是使用mongodb的多键索引方法来索引此数组项. 因此,给定 ProductCat 集合中某些文档的 ObjectId ,我可以列出当前集合中具有给定 ObjectId 的所有文档. _pro_cat 字段.

My schema has an array of ObjectIds which are refs to another schema. What i want is to index this array entry using multikey indexing method of mongodb. So that given an ObjectId of some document in the ProductCat Collection, i can list all the documents in my current collection which has the given ObjectId in the _pro_cat field.

我对猫鼬模式声明中应声明字段的确切方法感到困惑,这是我正在尝试的方法:

I am confused about the exact way in which i should declare the field, in mongoose schema declaration, Here is what i am trying:

_pro_cat: { type: [mongoose.Schema.Types.ObjectId], ref: 'ProductCat', index: true }

_pro_cat: [{ type: mongoose.Schema.Types.ObjectId, ref: 'ProductCat', index: true }]

此收藏集的名称是卖方.我通过使用架构中的引用来建立关系,并且实际字段值是一个数组,每个数组的类型为ObjectId,这将是另一个Collection(在这种情况下为ProductCat)中文档的ObjectId.如果我为此字段(即_pro_cat)建立索引,然后在ProductCat集合中给定了文档的ObjectId,则我将能够找到Seller集合中在其数组字段_pro_cat中具有ObjectId的所有文档.

The name of this collection is Seller. I am making the relation, by using the reference in the schema and the actual field value is an array, each of type ObjectId, which will be the ObjectIds of the documents in the other Collection (ProductCat in this case). If i index this field i.e.. _pro_cat, then given an ObjectId of a document in ProductCat Collection, i would be able to find all the documents in the Seller collection which has given ObjectId in its array field _pro_cat.

我认为我可能必须调用一个单独的索引函数.但是我认为这是字段级索引,因此可能不需要.

I think i may have to call a separate index function. But i thought that this is a field level indexing so it may not be needed.

我怀疑后者仅适用于子文档,而不适用于参考文献.如果有人能对此有所了解,将不胜感激.谢谢.

I suspect the later is only for sub-docs and not for refs. Would greatly appreciate, if anyone could shed some light on it. Thanks.

侧面问题:[已解决] 在mongodb文档中,它说多键索引是自动的. 多键索引.这是否意味着如果使用上述方法对该字段进行了索引,则mongo将识别该字段为数组并使用多键索引.或是否意味着所有数组类型的字段都将被索引,而无需明确地告诉它要索引.

Side Question: [RE-SOLVED] In the mongodb documentation, it says that multikey indexing is automatic. multikey indexing. Does this mean that if that field is indexed, using the above methods, then mongo will recognize that the field is an array and use multikey indexing. OR does it mean that all the fields which are of array type will get indexed, without the need to explicitly telling it to index.

非常感谢.

相关

推荐答案

多键索引 意味着,如果您告诉mongo对该字段进行索引,则会对数组字段中的每个项目都进行索引.

Multikey Indexing means that every item in an array-field is indexed if you tell mongo to index the field.

这并不意味着,所有数组字段都将自动索引,而无需明确告诉mongo对该字段进行索引.

It does not mean that all array fields are indexed automatically without the need to explicitely tell mongo to index the field.

这篇关于在mongoose中使用mongodb多键索引方法为引用建立索引数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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