2017-09-08 54 views
0

在MongoDB和文本过程中新的Im。 我有一个数据库与解析推文。 示例:MongoDB/PyMongo如何从数组中删除一个具体项目

{ 
    "_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" : "" 
} 

我将tweet中的词标记为词。 我的下一步是删除停用词。

我的代码:

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与停用词阵列。 我想从鸣叫的数组中删除这个项目,但我的代码失败:

raise WriteError(error.get("errmsg"), error.get("code"), error) pymongo.errors.WriteError: unknown top level operator: $eq

我不知道如果我的更新是正确的,你能帮帮我吗?

我最后objetive像(类似)的寄存器:

{ 
    "_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" : "" 
} 
+0

更新错误:提高WriteError(error.get(“ERRMSG”),error.get(“代码” ),错误) pymongo.errors.WriteError:未知顶级运算符:$ eq –

回答

0

您应该使用$in运营商不$eq。所以你不需要在for循环中控制每个停用词。您可以一次给所有的停止的话,拉他们都在一个这样的查询:

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