【问题标题】:MongoDB/PyMongo How to delete a concrete item from arrayMongoDB/PyMongo 如何从数组中删除具体项目
【发布时间】:2018-02-17 04:24:18
【问题描述】:

我是 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" : ""
}

【问题讨论】:

  • 更新错误:raise WriteError(error.get("errmsg"), error.get("code"), error) pymongo.errors.WriteError: unknown top level operator: $eq跨度>

标签: arrays mongodb twitter pymongo


【解决方案1】:

您应该使用$inoperator 而不是$eq。因此,您不需要在 for 循环中控制每个停用词。您可以一次给出所有停用词,然后将它们全部提取到一个查询中,如下所示:

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

【讨论】:

    猜你喜欢
    • 2013-06-08
    • 2021-10-19
    • 1970-01-01
    • 2014-09-03
    • 2016-09-07
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多