1

My mongo db sample is:

MONGO

    > db.pages.findOne()

{
"_id" : ObjectId("519b6e81661b820d0e5d4f83"),
"papers" : {
    "text" : "RT @sydest: Sütaş reklamlarındaki inekleri erkekler seslendirdiği sürece bu cinsiyet ayrımcılığı bitmez...",
    "ID" : null,
    "paragraphs" : [
        {
        "text" : "RT @sydest: Sütaş reklamlarındaki inekleri erkekler seslendirdiği sürece bu cinsiyet ayrımcılığı bitmez...",
        "ID" : "0P107",
        "sentences" : [
            {
            "text" : "RT @sydest: Sütaş reklamlarındaki inekleri erkekler seslendirdiği sürece bu cinsiyet ayrımcılığı bitmez...",
            "ID" : "0S107",
            "words" : [
                {
                "text" : "RT",
                "ID" : "1W3"
                    },
                    {
                    "text" : "sydest",
                    "ID" : "5W11"
                    },
                    {
                    "text" : "Sütaş",
                    "ID" : "13W18"
                    },
                    {
                    "text" : "reklamlarındaki",
                    "ID" : "19W34"
                    },
                    {
                    "text" : "inekleri",
                    "ID" : "35W43"
                    },
                    {
                    "text" : "erkekler",
                    "ID" : "44W52"
                    },
                    {
                    "text" : "seslendirdiği",
                    "ID" : "53W66"
                    },
                    {
                    "text" : "sürece",
                    "ID" : "67W73"
                    },
                    {
                    "text" : "bu",
                    "ID" : "74W76"
                    },
                    {
                    "text" : "cinsiyet",
                    "ID" : "77W85"
                    },
                    {
                    "text" : "ayrımcılığı",
                    "ID" : "86W97"
                    },
                    {
                    "text" : "bitmez",
                    "ID" : "98W104"
                    }
                ]
            }
        ]
    }
]
}
}

In this sample, i have one paper. In paper i have paragraphs key and as value sentences list. As same i have words key and as value words list in setences element.

I just want to get all "texts" which has "ID" with "W" letter. Shortly, i want to get all words in all document at once as a list or tuple. Thanks.

1 Answer 1

2

I'm pretty sure there's a more beautiful way to achieve what you want, but here's what I've came up to using find().

MongoDB query:

db.so.find({'papers.paragraphs': {$elemMatch: {'sentences': {$elemMatch: {'words': {$elemMatch: {'ID': {$regex: 'W'}}}}}}}}, {'papers.paragraphs.sentences.words.text': 1}).pretty();

python code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pymongo

mongo_db = pymongo.MongoClient().test

cursor = mongo_db.so.find({'papers.paragraphs':
                               {'$elemMatch':
                                    {'sentences':
                                         {'$elemMatch':
                                              {'words':
                                                   {'$elemMatch':
                                                        {'ID': {'$regex': 'W'}}}}}}}},
                          {'papers.paragraphs.sentences.words.text': 1})

results = []
for result in cursor:
    for paragraph in result['papers']['paragraphs']:
        for sentence in paragraph['sentences']:
            for word in sentence['words']:
                results.append(word['text'])

print results  # prints [u'RT', u'sydest', ... ]

Hope that helps.

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

Comments

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.