在子文档字段通配符上创建文本索引 [英] Create text index on sub-document field wildcard

查看:110
本文介绍了在子文档字段通配符上创建文本索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MongoDB中为子文档中的多个字段创建文本索引.这些字段在文档之间是不同的. 根据文档,我将创建像这样的普通文本索引:

I am experimenting with creating a text index in MongoDB for several fields in a sub-document. These fields are not the same from document to document. According to the documentation, I would create a normal text index like so:

db.collection.ensureIndex({
    subject: "text",
    content: "text"
});

就我而言,我希望db.fs.files.metadatafs.files集合中所有字段的索引.我已经尝试过了:

In my case, I want the index on all fields in the fs.files collection at db.fs.files.metadata. I've tried this:

db.fs.files.ensureIndex({'metadata.$**': 'text'});

我不认为这行得通,因为使用db.fs.files.runCommand('text'...搜索将返回零结果,并且db.fs.files.stats()将索引显示为非常小的大小(并且在此集合中,我有约3.5万个文档).

I don't believe this has worked, as searching with db.fs.files.runCommand('text'... returns zero results, and db.fs.files.stats() shows the index as a very small size (and I have ~35k documents in this collection).

如何在事先不知道键的子文档的字段值上创建文本索引?

How can I create a text index on field values of a subdocument where the keys are not known ahead of time?

推荐答案

如果在父文档上创建{'$ **':'text'}索引,它也会对子文档字段建立索引.文档说,它只会影响文字,因此会跳过文件数据,但会包含名称& contentType.

If you create a {'$**': 'text'} index on the parent document it will index the subdocument fields too. The docs say it only affects text so it will skip the file data but will include the name & contentType.

这篇关于在子文档字段通配符上创建文本索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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