1

I have a collection of documents representing messages. Each message has multiple fields that change from message to message. They are stored in a "fields" array of sub-documents.

Each element in this array contains the label and value of a field. Some fields may contain long lists of strings (IP addresses, URLs, etc.) - each string appears in a new line within that field. Lists can be thousands of lines long. For that purpose, each element also stores a "type" - type 1 represents a standard text, while type 2 represents a list. When there's a type 2 field, the "value" in the sub-document is an array of the list.

It looks something like this:

"fields" : [
{
    "type" : 1,
    "label" : "Observed on",
    "value" : "01/09/2016"
},
{
    "type" : 1,
    "label" : "Indicator of",
    "value" : "Malware"
},
{
    "type" : 2,
    "label" : "Relevant IP addresses",
    "value" : [
        "10.0.0.0",
        "190.15.55.21",
        "11.132.33.55",
        "109.0.15.3"
    ]
}

]

I want all fields values to be searchable and indexed, whether these values are in a standard string or in an array within "value".

Would setting up a standard index on "fields.value" index both type 1 and type 2 content? do I need to set up two indexes?

Thanks in advance!

1 Answer 1

1

When creating a new index, mongodb will automatically switch to Multikey index if it stumbles across an array in a document on the indexed field. Which means that simply:

collection.createIndex( { "fields.value": 1 } )

should work just fine.

see: https://docs.mongodb.com/v3.2/core/index-multikey/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I looked into the documentation but must have missed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.