FAQ Database Discussion Community


MongoDB: How to find a document by an id inside a nested document

mongodb,mongodb-query,embedded-documents
Given a collection like this:.. [ { "_id" : ObjectId("5546329a470000850084a621"), "name": "Joe", "surname": "Smith", "accounts": [ { "_id" : ObjectId("5546329a470000850084a655"), "default": true, "status" : "approved", "activationTime" : ISODate("2013-05-03T14:37:15.025Z") }, { "_id" : ObjectId("5546329a470000850084a688"), "default": true, "status" : "approved", "activationTime" : ISODate("2014-06-03T14:37:15.025Z") } ] }, { "_id" : ObjectId("9546329a470079850084a622"), "name": "Jimmy",...

Mongoid HABTM relationships across embedded documents

ruby-on-rails,ruby-on-rails-4,mongoid,embedded-documents
I'm trying to create a Mongoid N-N reference association between two embedded documents in Rails 4 however I'm finding it difficult to get my head round how this is done. I started by adding the HABTM association to the relevant models below (Track and Option) but of course I'm getting...

Query an array of embedded documents in mongodb

mongodb,embedded-documents
I'm having a little trouble writing a query that needs to compare a given value against a certain field in all embedded documents within an array. I will give an example to make the issue less abstract. Let's say I want to use MongoDB to store the last queries that...

What does indexing a key of an embedded document really mean?

arrays,node.js,mongodb,indexing,embedded-documents
I still can't understand how indexing a key of an embedded document really works. Suppose I have the following collection of blog posts: { _id:0, author: 'John Doe', content: 'How indexing an embedded document work?', comments: [ { sender:'Jane Doe', content: 'I can\'t make it out either.' }, etc... ]...

$elemMatch on embedded documents

mongodb,embedded-documents
Here is what I am working with: { "_id": ObjectId("53b4402ae1382335c95952d3"), "created": ISODate("2014-07-02T10:23:54.245Z"), "modified": ISODate("2011-11-28T10:06:25.186Z"), "source": [{ "instances": [{ "analyst": "harry", "date": ISODate("2014-07-02T10:23:54.242Z"), "reference": "abb32" }, { "analyst": "david", "date": ISODate("2014-07-02T10:33:02.296Z"), "reference": "can26" }], "name": "Apples" }, { "instances": [{ "analyst": "critsAPI", "date": ISODate("2011-11-28T10:06:25.156Z"), "reference": "z561c" }], "name": "Oranges" }],...