繁体   English   中英

如何从python pymongo,MongoDB中找到特定的key:value 2

[英]How to find specific key:value 2 from python pymongo, MongoDB

我的mongo db示例是:

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"
                    }
                ]
            }
        ]
    }
]
}
}

在这个样本中,我有一篇论文。 在论文中,我有段落关键字和值句子列表。 同样,我在setences元素中有单词key和值单词列表。

我只想获取所有带有“ ID”和“ W”字母的“文本”。 不久,我想一次获得所有文档中所有单词的列表或元组。 谢谢。

我敢肯定,有一种更漂亮的方法可以实现您想要的,但是这就是我使用find()想法。

MongoDB查询:

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

python代码:

#!/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', ... ]

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM