Indexing multiple fields in mongoDB

I need to store information per object in MongoDB. Each object can have a variable number of attributes, and each attribute name can also be different.

Object 1:

  • name: "Cabinet number 1"
  • Type: "Locker"
  • material: "Wood"
  • dimensions: "12X15"
  • built on: "Oct 2, 2014"
  • id: 12212

Object 2:

  • name: "Tommy"
  • type: "Pet"
  • owner: "Tom"
  • Born: June 1, 2010
  • id: 12321

Thus, each object can have different attributes / fields. I would like to be able to query this database by filtering any combination of attributes. Since the database can be huge, I would like the data retrieval to be fast, for which I would like some sort of indexing to be possible, which makes the query faster.

An example request could be:

  • Find all records that have Material = "Wood" and Type = "Table"
  • Search all records that have type = "Dog" and owner = "Harry"

I was thinking about using a collection where all the attributes can fit into a single object, but can't see how the index could be. I can obviously split the object into separate attribute objects and then store it, which would mean something like this:

Object 1 has several separate objects:

  • {"attr_name": "name", "attr_value": "Cabinet number 1", "id": 12212}
  • {"attr_name": "type", "attr_value": "Wardrobe", "id": 12212}
  • {"attr_name": "material", "attr_value": "Wood", "id": 12212}
  • {"attr_name": "dimensions", "attr_value": "12X15", "id": 12212}
  • {"attr_name": "built on", "attr_value": "October 2, 2014", "id": 12212}

Now I can create an index on attr_name and get an 'id' which I can use to collect all records for this object. However, this won't work outside of one attribute, while my requirement is to filter multiple attributes in one request.

I would like to structure the whole object somehow (without breaking it like here) and still be able to create indexes. Any hints on how to do this? How to solve such problems? Are there any NOSQL / other tools to deal with such problems? I'm open to using a different database if that's one way or another.

0


source to share


1 answer


MongoDB has a reference to a standard architecture for a retail implementation that reflects the problem you are trying to solve. It also talks about indexing and displaying faceted search based on the attributes of the query variations. I think you will find very important information there and a good starting point for solving your problem:



https://www.mongodb.com/blog/post/retail-reference-architecture-part-1-building-flexible-searchable-low-latency-product

0


source







All Articles