2013-08-30 3 views
0

Je suis en train de mettre à jour quelques champs dans un tableau à l'intérieur de tableauMettre à jour quelques champs tableau imbriqué dans MongoDB (avec pymongo)

Un exemple de document est comme ceci:

{ 
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 
    } 
], 

}

Je veux mettre à jour le tableau urls comme:

 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" 
      } 
     ], 

J'utilise quelque chose comme ça pour faire la mise à jour:

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) 

Cependant, il me donne l'erreur

can't append to array using string field name [urls] 

Toutes les suggestions?

Répondre

0

Je ne peux pas être certain, mais cela pourrait être ce qui se passe. A partir de votre document exemple, votre modèle a ce schéma:

{ 
    id: Number, 
    tweets: Array 
} 

Lorsque vous recherchez l'instance du modèle dans

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

Il est probable que ce n'est pas trouver l'instance du modèle que vous recherchez pour.

Je voudrais essayer de lancer ce script pour voir si vos critères de recherche sont valables:

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

Hope it helps!

Questions connexes