2017-09-08 2 views
0

MongoDB 및 텍스트 프로세스에서 새로 추가되었습니다. 구문 분석 된 트윗이있는 데이터베이스가 있습니다. 예 :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" : "" 
} 

트윗에 단어를 토큰 화했습니다. 다음 단계는 불용어를 삭제하는 것입니다.

내 코드 :

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

, 당신이 나를 도와 드릴까요? 당신은 $in 운영자하지 $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" : "" 
} 
+0

업데이트 오류 : WriteError (error.get ("errmsg를"), error.get ("코드"를 제기), 오류) pymongo.errors.WriteError : 알 수없는 최상위 연산자 : $ eq –

답변

0

:

내 최종 objetive는 (유사)와 같은 레지스터입니다. 따라서 for 루프에서 각 중지 단어를 제어 할 필요가 없습니다. 당신은 한 번에 모든 중지 단어를주고 같은 하나 개의 질의에 그들 모두를 뽑을 수 :

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

관련 문제