2012-04-30 28 views
0

如果我有使用以下結構的幾個文件:MongoDB中查找所有評論

{ 
    "_id" : ObjectId("4f9c6fe033cddec1bea430f2"), 
    "title" : "Product #1", 
    "comments" : 
    [ 
    { 
    "comment" : "My comment", 
    "date" : "2012-03-01 00: 00: 00", 
    "name" : "Test Name", 
    "username" : "Test Username" 
    }, 
    { 
    "comment" : "My comment", 
    "date" : "2012-03-01 00: 00: 00", 
    "name" : "Test Name 2", 
    "username" : "Test Username 2" 
    } 
    ] 
} 

是否有可能對我來說,找到有reviews.name並更新所有文件?例如說,我想查找所有具有user.name的文件:test name 2並將其更改爲Cassie。那可能嗎?如果不是最好將評論放在不同的集合中?

回答

2

是的,這是可能的。你將不得不使用comments.username找到它,然後comments.$.username請注意,因爲你張貼到一個數組,你必須告訴mongo去那裏使用$ positional operator

+0

感謝您的幫助 – cKendrick

相關問題