1

I'm new bee in mongodb. I made a nested array document like this.

data = {
    "title": "mongo community",
    "description": "I am a new bee",
    "topics": [{
        "title": "how to find object in array",
        "comments": [{
            "description": "desc1"
        }]
    },
    {
        "title": "the case to use ensureIndex",
        "comments": [{
            "description": "before query"
        },
        {
            "description": "If you want"
        }
        ]
    }
    ]
}

after that, put it in the "community" db.community.insert(data)

so,I would like to accumulate "comments" which topics title is "how to find object in array" then I tried,

data = db.community.find_one({"title":"mongo community","topics.title":"how to find object in array" } )

the result is

>>> print data
{
    u 'topics': [{
        u 'comments': [{
            u 'description': u 'desc1'
        }],
        u 'title': u 'how to find object in array'
    },
    {
        u 'comments': [{
            u 'description': u 'before query'
        },
        {
            u 'description': u 'If you want'
        }],
        u 'title': u 'the case to use ensureIndex'
    }],
    u '_id': ObjectId('4e6ce188d4baa71250000002'),
    u 'description': u 'I am a new bee',
    u 'title': u 'mongo community'
}

I don't need the topics "the case to use ensureIndex"

Whould you give me any advice.

thx.

2 Answers 2

4

It looks like you're embedding topics as an array all in a single document. You should try to avoid returning partial documents frequently from MongoDB. You can do it with the "fields" argument of the find method, but it isn't very easy to work with if you're doing it frequently.

So to solve this you could try to make each topic a separate document. I think that would be easier for you too. If you want to save information about the "community" for forum, put it in a separate collection. For example, you could use the following in the monbodb shell:

// ad a forum:
var forum = { 
                title:"mongo community",
                description:"I am a new bee"
            };
db.forums.save(forum);

// add first topic:
var topic = {
                title: "how to find object in array",
                comments: [ {description:"desc1"} ],
                forum:"mongo community"
            };
db.topics.save(topic);

// add second topic:
var topic = {
                title: "the case to use ensureIndex",
                comments: [ 
                    {description:"before query"},
                    {description:"If you want"}
                ],
                forum:"mongo community"
            };
db.topics.save(topic);

print("All topics:");
printjson(db.topics.find().toArray());


print("just the 'how to find object in array' topic:")
printjson(db.topics.find({title:"how to find object in array"}).toArray());

Also, see the document Trees In MongoDB about schema design in MongoDB. It happens to be using a similar schema to what you are working with and expands on it for more advanced use cases.

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

3 Comments

Sure thing Tsutomu. Is there anything else I can answer for you related to this? If so please add a comment. If everything is answered, it will be great if you can mark this answer as the answer.
Thank you very much scott! It seems very simple. I would't solve the problem by myself. I was believed that MongoDB is rich document. But I should use it effectively. Thanks again!
Glad it was helpful to you. Just a note about how we use stackoverflow here. So that people know that your question was answered, you can mark it as answered by clicking the checkbox. More information about using stackoverflow is at stackoverflow.com/faq#howtoask .
0

MongoDB operates on documents, that is, the top level documents (the things you save, update, insert, find, and find_one on). Mongo's query language lets you search within embedded objects, but will always return, update, or manipulate one (or more) of these top-level documents.

MongoDB is often called "schema-less," but something more like "(has) flexible schemas" or "(has) per-document schemas" would be a more accurate description. This is a case where your schema design -- having topics embedded directly within a community -- is not working for this particular query. However there are probably other queries that this schema supports more efficiently, like listing the topics within a community in a single query. You might want to consider the queries you want to make and re-design your schema accordingly.

A few notes on MongoDB limitations:

  1. top-level documents are always returned (optionally with only a subset of fields, as @scott noted -- see the mongodb docs on this topic)
  2. each document is limited to 16 megabytes of data (as of version 1.8+), so this schema will not work well if the communities have a long list of topics

For help with schema design, see the mongodb docs on schema design, Kyle Banker's video "Schema Design Basics", and Eliot Horowitz's video "Schema Design at Scale" for an introduction, tips, and considerations.

1 Comment

Thank you so much dcrosta, the documents was so helpful.

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.