2013-08-30 97 views
0

我試圖陣列內更新陣列內的幾個字段更新MongoDB中嵌套陣列幾個字段(pymongo)

一個實例文件是這樣的:

{ 
id: 987654321 
tweets: [ 
    { 
     text: "RT @947FreshFM: A vigil will be held for #SandyHook victims at UMd. at 7pm at Nyumburu Ampitheater. @BlackTerp", 
     urls: [ 

     ], 
     id: 279718351289348100 
    }, 
    { 
     text: "RT @WTOP: McDonnell: If you talk completely about guns, I think you're missing the point. #AskTheGov http://t.co/hbFt7t1n", 
     urls: [ 
      { 
       status: null, 
       domain: null, 
       url: "http://t.co/hbFt7t1n", 
       period: null, 
       resolved_url: null, 
       annotation: null 
      } 
     ], 
     id: 281061376275906560 
    } 
], 

}

我想更新的URL數組:

 urls: [ 
      { 
       status: null, 
       domain: "wtop.com", 
       url: "http://t.co/hbFt7t1n", 
       period: null, 
       resolved_url: "http://wtop.com/?nid=610&sid=3162096", 
       annotation: null, 
       annotation2: "guncontrol" 
      } 
     ], 

我使用的是這樣做的更新:

collection.update({"id":987654321, "tweets.id":281061376275906560,"tweets.urls.url":"http://t.co/hbFt7t1n"}, 
{"$set":{ 
    "tweets.urls.resolved_url":"http://wtop.com/?nid=610&sid=3162096", 
    "tweets.urls.domain": "wtop.com", 
    "tweets.urls.annotation2":"guncontrol" 
}}, False,False) 

但是它給了我錯誤

​​

有什麼建議?

回答

0

我不能確定,但​​這可能是怎麼回事。從您的示例文件,你的模型具有此架構:

{ 
    id: Number, 
    tweets: Array 
} 

當您在

collection.update({ 
    "id": 987654321, 
    "tweets.id": 281061376275906560, 
    "tweets.urls.url": "http://t.co/hbFt7t1n" 
}, ...)` 

搜索模式的實例這可能是因爲它沒有發現該模型的情況下,你正在尋找對於。

我會嘗試運行此腳本,看看您的搜索條件是有效的:

console.log(collection.find({ 
    "id": 987654321, 
    "tweets.id": 281061376275906560, 
    "tweets.urls.url": "http://t.co/hbFt7t1n" 
})); 

希望它能幫助!