MongoDB efficient dealing with embedded documents

Posted by Sebastian Nowak on Stack Overflow See other posts from Stack Overflow or by Sebastian Nowak
Published on 2014-08-23T16:02:40Z Indexed on 2014/08/23 16:20 UTC
Read the original article Hit count: 242

Filed under:
|

I have serious trouble finding anything useful in Mongo documentation about dealing with embedded documents. Let's say I have a following schema:

{
  _id: ObjectId,
  ...
  data: [
    {
      _childId: ObjectId // let's use custom name so we can distinguish them
      ...
    }
  ] 
}
  1. What's the most efficient way to remove everything inside data for particular _id?

  2. What's the most efficient way to remove embedded document with particular _childId inside given _id? What's the performance here, can _childId be indexed in order to achieve logarithmic (or similar) complexity instead of linear lookup? If so, how?

  3. What's the most efficient way to insert a lot of (let's say a 1000) documents into data for given _id? And like above, can we get O(n log n) or similar complexity with proper indexing?

  4. What's the most efficient way to get the count of documents inside data for given _id?

© Stack Overflow or respective owner

Related posts about Performance

Related posts about mongodb