MongoDB / PyMongo如何从数组

时间:2017-09-08 09:29:10

标签: arrays mongodb twitter pymongo

我是MongoDB和Text流程的新手。 我有一个带解析推文的数据库。 例如:

{
    "_id" : ObjectId("59b24aa1a0c99b0b85732406"),
    "idt" : "906060929829183489",
    "tweet" : [
        "RT",
        "@moocowpong1",
        ":",
        "@whitequark",
        "isn't",
        "the",
        "cloud",
        "just",
        "your",
        "data",
        "relocating",
        "to",
        "san",
        "francisco"
    ],
    "createdDate" : ISODate("2017-09-08T07:45:34Z"),
    "userName" : "Fiora Aeterna",
    "userLocation" : "San Jose, CA",
    "geo" : null,
    "geoCoord" : null,
    "Lang" : "en",
    "retweet_count" : 0,
    "sentimiento" : "",
    "score_tag" : ""
}

我对推文中的文字进行了标记。 我的下一步是删除停用词。

我的代码:

for doc in tweets.find({},{'tweet': 1}).limit(1):
    print (doc)
    for term in (doc['tweet']):
        if set(stop).intersection(term.split()):
            print ("Found One")
            tweets.update( { 'idt': doc['_id'] }, { '$pull': { 'tweet': { '$eq': term } } } )

stop是一个带停用词的数组。 我想从推文的数组中删除该项,但我的代码失败了:

  

引发WriteError(error.get(" errmsg"),error.get(" code"),error)   pymongo.errors.WriteError:未知顶级运算符:$ eq

我不确定我的更新是否正确,你能帮助我吗?

我最后的目标是寄存器(类似):

{
    "_id" : ObjectId("59b24aa1a0c99b0b85732406"),
    "idt" : "906060929829183489",
    "tweet" : [
        "@moocowpong1",
        "@whitequark",
        "cloud",
        "just",
        "data",
        "relocating",
        "san",
        "francisco"
    ],
    "createdDate" : ISODate("2017-09-08T07:45:34Z"),
    "userName" : "Fiora Aeterna",
    "userLocation" : "San Jose, CA",
    "geo" : null,
    "geoCoord" : null,
    "Lang" : "en",
    "retweet_count" : 0,
    "sentimiento" : "",
    "score_tag" : ""
}

1 个答案:

答案 0 :(得分:0)

您应该使用$in运算符而不是$eq。因此,您不需要在for循环中控制每个停用词。您可以立即给出所有停用词,并在一个查询中提取所有这些词:

db.collection.update({}, { $pull: { "tweet": { $in: ["stopWord1", "stopWord2"] } } } )