F. Santiago
F. Santiago

Reputation: 850

MongoDB performance. Embedded documents search speed

I was wandering what keep MongoDB faster. Having a few parent documents with big arrays of embedded documents inside of them or having a lot of parent documents with few embedded documents inside.

This question only regards querying speed. I'm not concerned with the amount of repeated information, unless you tell me that it influences the search speed. (I don't know if MongoDb automatically indexes Id's)

Example:

Having the following Entities with only an Id field each one:

In order to associate students with classes, would I be taking most advantage of MongoDB's speed if I:

This example is just an example. A real sittuation would involve thousands of documents.

Upvotes: 2

Views: 2452

Answers (1)

Thilo
Thilo

Reputation: 262474

I am going to search for specific students inside a given class.

If so, you should have a Student collection, with a field set to the class (just the class id is maybe better than an embedded and duplicated class document).

Otherwise, you will not be able to query for students properly:

db.students.find ({ class: 'Math101', gender: 'f' , age: 22 })

will work as expected, whereas storing the students inside the classes they attend

{ _id: 'Math101', student: [
     { name: 'Jim', age: 22 } , { name: 'Mary', age: 23 }
  ] }

has (in addition to duplication) the problem that the query

db.classes.find ( { _id: 'Math101', 'student.gender': 'f', 'student.age': 22 })

will give you the Math class with all students, as long as there is at least one female student and at least one 22-year-old student in it (who could be male).

You can only get a list of the main documents, and it will contain all embedded documents, unfiltered, see also this related question.

I don't know if MongoDb automatically indexes Id

The only automatic index is the primary key _id of the "main" document. Any _id field of embedded documents is not automatically indexed, but you can create such an index manually.

Upvotes: 2

Related Questions