0

I'm trying to minimize my document fields.

This is a sample document:

{_id: "T7BWYnDoozmFEhLW3", no: "3748597", letter: "I", extra: []}

If I populate _id with no and letter, it would be like this:

{_id: "3748597_I", extra: []}

So to get no and/or letter fields, I would use $regex on _id.

Would this slow down the searching process?

stackyname
  • 314
  • 3
  • 10
  • 2
    you can test with explain(), to know the performance – achuth May 02 '16 at 12:01
  • @achuth Thanks a lot! – stackyname May 02 '16 at 12:27
  • There are many existing questions on `$regex` and indexes, so this is likely a duplicate. A good starting point is the [Index Use](https://docs.mongodb.org/manual/reference/operator/query/regex/#index-use) section of the `$regex` query operator documentation. A `$regex` search for a case-sensitive prefix can efficiently use an index, but non-prefix or case-insensitive searches (eg. matching on the `no` suffix alone) will result in an index scan. The outcome on your query performance will depend on frequency and type of `$regex` queries you expect to be running, and the size of the collection. – Stennie May 03 '16 at 03:55
  • You could also consider a composite `_id` key (see discussion on: [MongoDB and composite primary keys](http://stackoverflow.com/questions/23164417/mongodb-and-composite-primary-keys)), but if you need to search on the individual fields in `_id` (`_id.letter` and `_id.no`) this will be adding complexity without saving any indexes. – Stennie May 03 '16 at 03:56

0 Answers0