问题描述
我们正在存储结构未定义的文档.我的意思是,它具有基本结构(id
,user
和creationTimestamp
),但也有一个Map<String, Object> values
字段,我们可以在其中存储任何结构:
We're storing documents with a undefined structure. I mean, it has a basic structure (id
, user
and creationTimestamp
), but is also there a Map<String, Object> values
fields, where we are able to store whichever structure:
public class Metadata {
private String id;
private String user;
private Date creationTimestamp;
private Map<String, Object> values;
}
示例:
> db.metadata.find();
{
"_id" : "Doc2Ref2Mdt1",
"user" : "user1",
"creationTimestamp" : ISODate("2018-09-24T12:20:56.958Z"),
"values" : {
"ambit" : "ctti",
"departament" : "economia"
}
},
{
"_id" : "Doc1Ref2Mdt1",
"user" : "user2",
"creationTimestamp" : ISODate("2018-09-24T12:20:56.169Z"),
"values" : {
"date" : ISODate("2018-09-24T12:20:56.171Z"),
"number" : 16,
"address" : {
"street" : "Av. Diagonal",
"location" : "barcelona",
"credentials" : [
{
"password" : "pwd",
"login" : "main"
},
{
"password" : "pwd",
"login" : "other",
"creation" : ISODate("2018-09-24T12:20:56.171Z")
}],
"contact" : "contact name",
"tags" : ["tag1", "tag2"}]
}
}
因此,您可以看到values
可以存储任何结构.
So, you can see values
can store any structure.
我需要知道mongodb是否能够自动为所有索引.
I need to know if mongodb is able to automatically index all of them.
我的意思是,当将一个新字段添加"到values
中时,例如,values.newfield
它会自动被索引.
I mean, when a new field is "added" into values
, for example, values.newfield
it is indexed automatically.
有什么想法吗?
推荐答案
以您想要的方式无法实现.
Not possible in the way you want it.
您可以在所有字段上尝试文本索引,然后是实际查询.单独的文本搜索可能会导致假阳性匹配,但与常规查询聚合在一起会减少要扫描的数据集,并且在大多数情况下会加快扫描速度.
You can try text index on all fields followed by the actual query. The text search alone may result with false-positive matches, but aggregated with the normal query it will reduce the dataset to scan and in most cases will speed it up.
要牢记一些限制:
- 仅字符串字段将被索引,例如
.find({ $text: { $search: "2018-09-24" } })
将不会返回任何内容..find({ $text: { $search: "16" } })
都不会 - 仅全字查询,即无正则表达式.您将需要使用
language: "none"
保留停用词,并且不要使用词干.像.find({ $text: { $search: "barcel" } })
这样的查询将找不到第二个文档.
- only string fields will be indexed, e.g.
.find({ $text: { $search: "2018-09-24" } })
won't return anything. Neither will.find({ $text: { $search: "16" } })
- only full words queries, i.e. no regexps. You will need to use
language: "none"
to keep stopwords and don't use steming. Queries like.find({ $text: { $search: "barcel" } })
won't find the second document.
如果您控制所有写入操作,则可以通过将values
映射序列化为写入时的字符串字段来解决第一个限制.然后,您将仅需要在此字段上创建文本索引.
The first limitation can be worked around by serialisation of values
map to a string field on write, if you control all writes. Then you will need to create a text index on this field only.
这篇关于MongoDB:如何索引未知字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!