2017-04-11 82 views
0
{ 
    "doc_id": 1234, 
    "pars": [ 
     { 
     "par_id": 4567, 
     "someNode": "...", 
     "lines": [ 
      { 
      "line_id": 8901, 
      "someOtherNode": "...", 
      "text": "Foo" 
      }, 
     ] 
     } 
    ] 
} 

我怎样才能在正确的节点知道doc_idpar_idline_id更新text领域的节点?匹配和更新根据其内容

我不能使用此查询,因为我不具有对应于par_idline_id

con.db.col.update(
    {'_id': doc_id}, 
    {'$set': { "pars.?.lines.?.text": "Bar" } } 
) 
+0

可能欺骗http://stackoverflow.com/questions/30019015/update-nested-array-document – chridam

+0

另外值得跟踪JIRA https://jira.mongodb.org/browse/SERVER-831 – chridam

回答

-1

,你可以不喜欢这个 -

db.col.update(
    {"_id": doc_id, "par_id": 4567, "pars.lines.line_id":8901}, 
    {$set: { "pars.$.lines.$.text":"ANY_TEXT_YOU_WANT" } }, 
function(){} //callback function) 
+0

不,不支持两个“$”操作符。 –

+0

OOPS使用这个 - : '{$ set:{“pars.lines。$。text”:“ANY_TEXT_YOU_WANT”}} – hardy

0

Because updating based on a match in nested arrays is not yet supported in MongoDB节点索引,你”你需要重新安排你的模式。现在你有一个段落数组,每个段落都是一行数组。我建议制作段落号为的文档,其字段名为paragraphIds,而不是数组。你可以保持行作为一个数组,你可以通过paragraphId和lineId使用"$" positional operator然后更新:

> db.collection.insertOne({ 
... "doc_id": 1234, 
... "pars": { 
...  4567: { 
...  "someNode": "...", 
...  "lines": [ 
...   { 
...   "line_id": 8901, 
...   "someOtherNode": "...", 
...   "text": "Foo" 
...   }, 
...  ] 
...  } 
... } 
... }) 
{ 
    "acknowledged" : true, 
    "insertedId" : ObjectId("58ecb95e475838c0db6efd9d") 
} 
> db.collection.updateOne({ 
... "doc_id": 1234, 
... "pars.4567.lines.line_id": 8901 
... }, { 
... "$set": {"pars.4567.lines.$.text": "Bar"} 
... }) 
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } 
> db.collection.findOne({"doc_id": 1234}) 
{ 
    "_id" : ObjectId("58ecb95e475838c0db6efd9d"), 
    "doc_id" : 1234, 
    "pars" : { 
    "4567" : { 
     "someNode" : "...", 
     "lines" : [ 
     { 
      "line_id" : 8901, 
      "someOtherNode" : "...", 
      "text" : "Bar" 
     } 
     ] 
    } 
    } 
} 
-1

$$是特殊的运算符(新位置运营商)

你可以做这样的事情 -

db.col.update(
    {"_id": doc_id, "par_id": 4567, "pars.lines.line_id":8901}, 
    {$set: { "pars.lines.$$.text":"ANY_TEXT_YOU_WANT" } }, 
function(){} //callback function)