1

Hi i´m completely newbie with mongodb, i come from SQL Server, i have the following doubt If a have the structure bellow:

Collection: tv

"_id": ObjectId("123456abc"),
"brand": "Sony",
"model": "Bravia",
"price": 1000

Collection: tvcomments

"_id": "_id": ObjectId("456789def"),
"tv": ObjectId("123456abc"),
"comments": [
         {
           "user": ObjectId("413212eop"),
           "text": "Very nice TV"
         }
]

I´d like to get tv with their comments, but i don´t find a example to do this, maybe is not possible?

3
  • 1
    db.tvcomments.find({tv:object_id_of_tv_document})? Commented Aug 26, 2013 at 16:53
  • Can always just create another field in the TV collection called "comments", then just pull the info from there. Otherwise, refer to @Sammaye s snippet Commented Aug 26, 2013 at 16:55
  • You can only retrieve documents from one collection at a time. So you can't get them both in a single query without restructuring your documents or collections. Commented Aug 26, 2013 at 17:22

1 Answer 1

3

One difference between MongoDB and SQL is that there are no JOINs in MongoDB. The preferred way to model data in MongoDB is to embed documents inside each other instead of separating them out and referencing them by ObjectId.

Your current way of doing things will require two trips to the database, one to get the TV and one to get the comments. Unless you're planning to reference the comments in a place where you wouldn't be referencing the TV (unlikely) then you can just put the comments in the TV document directly. Now you get the comments "for free" when you pull the TV documents.

{
    "_id": ObjectId("123456abc"),
    "brand": "Sony",
    "model": "Bravia",
    "price": 1000,
    "comments": [
             {
               "user": ObjectId("413212eop"),
               "text": "Very nice TV"
             }
    ]
}

Notice that I left the reference to the user instead of embedding that in the TV document. There are cases where you'll need to just perform multiple queries to get your data because embedding is not the proper relationship.

See here for more info: http://docs.mongodb.org/manual/core/data-modeling/

Sign up to request clarification or add additional context in comments.

2 Comments

I was reading the mongodb docs, in concrete about manual reference and it says the following: Manual references refers to the practice of including one document’s _id field in another document. The application can then issue a second query to resolve the referenced fields as needed I don´t understand the objective of this reference or DBRef, it only shows me a reference but it not bring me the info that i require. I need to have the tvs in one collection and the users in another collection, i can´t embed the user information (name, image...) in the tv collection.
MongoDB might not be the right technology for you if you have highly relational data. In this case your application would need to query for the TV object, then get a list of users that had commented, then make a second query for those users. There is no way to bring back all that data in one query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.