Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-831

Positional Operator Matching Nested Arrays

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.5.12
    • Affects Version/s: None
    • Component/s: Querying, Write Ops
    • Labels:
      None
    • Fully Compatible

      Issue Status as of Aug 11, 2017

      FEATURE DESCRIPTION
      MongoDB 3.5.12 extends all update modifiers to apply to all array elements or all array elements that match a predicate, specified in a new update option arrayFilters. This syntax also supports nested array elements.

      VERSIONS
      This new feature is available starting with the MongoDB 3.5.12 development version, and included in the MongoDB 3.6 production version.

      OPERATION

      Update all documents in array

      db.coll.update({}, {$set: {“a.$[].b”: 2}})
      Input: {a: [{b: 0}, {b: 1}]}
      Output: {a: [{b: 2}, {b: 2}]}
      

      Update all matching documents in array

      db.coll.update({}, {$set: {“a.$[i].b”: 2}}, {arrayFilters: [{“i.b”: 0}]})
      Input: {a: [{b: 0}, {b: 1}]}
      Output: {a: [{b: 2}, {b: 1}]}
      

      Update all matching scalars in array

      db.coll.update({}, {$set: {“a.$[i]”: 2}}, {arrayFilters: [{i: 0}]})
      Input: {a: [0, 1]}
      Output: {a: [2, 1]}
      

      Update all matching documents in nested array

      db.coll.update({}, {$set: {“a.$[i].c.$[j].d”: 2}}, {arrayFilters: [{“i.b”: 0}, {“j.d”: 0}]})
      Input: {a: [{b: 0, c: [{d: 0}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}
      Output: {a: [{b: 0, c: [{d: 2}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}
      

      Update all scalars in array matching a logical predicate

      db.coll.update({}, {$set: {“a.$[i]”: 2}}, {arrayFilters: [{$or: [{i: 0}, {i: 3}]}]})
      Input: {a: [0, 1, 3]}
      Output: {a: [2, 1, 2]}
      

      Each array filter must be a predicate over a document with a single field name. Each array filter must be used in the update expression, and each array filter identifier $[<id>] must have a corresponding array filter. <id> must begin with a lowercase letter and not contain any special characters. There must not be two array filters with the same field name.

      IMPLEMENTATION DETAILS
      The implementation of this feature involved a rewrite of the update system. Users can find all the related tickets here. The design document is attached.

      Original description

            Assignee:
            tess.avitabile@mongodb.com Tess Avitabile (Inactive)
            Reporter:
            durran Durran Jordan
            A. Jesse Jiryu Davis, abdelouahab, Adam Reis, Alberto Lerner, Alexander, Alexander Regueiro, alex rockwell, Alex Young, Andreas B., Andrew Kalek, antoine, Anton Khodakivskiy, ashraf fayad, Asya Kamsky, Barry Dobson, basile, Binard, Bonozo Games, Brett [X], Bryan Migliorisi, Carl Banbury, Charlie Stigler, Chris Sedlmayr, Coen Hyde, Dani, Daniel Ciupe, Daniel Doubrovkine, Daniel Macias, Daniel Pasette, Dave Geddes, David Creixell, david zhang, Dennis P, Devin Mooers, Dissatisfied Former User, Durran Jordan, Eliot Horowitz, Ezekiel Victor, Frank Mayer, Freddy Löckli, Greg Braman, Guihgo [X], Guilherme Koehler, Guy Korland, Hans Petee, Hugh Watkins, I Need This Feature [X], Ioannis Chouklis, Ivan Fioravanti, Jake Clarkson, Jeff Whelpley, Joe, john.nishinaga@patdeegan.com, Jonathan, Jonathan Chambers, Joseph Blair, Joshua Austill, Joshua Chan, Justin Dearing, Keith Branton, Ketan K Naik, KimShen, Kyle Richter, Maniratnam Katakam, Manjunath Bhuyar, Marc MacLeod, Markus Graf, Markus Seeger, Matt Bodman, Megha Dev, Michael Fitchett, Michael Joyce, Michael Karpitsky, Miguel, Miko?aj Michalczyk, Mohammad Musa, Morten Herman Langkjaer, Murilo Lobato, neeraj wadhwani, Neil Bartlett, Prashant Tiwari, Rainer Schreiber, Raul Guerrero, Ravindra M Rao, Remon van Vliet, Robert Weissmann, Roman, Rydal, S?awomir Nowak, Samuel Iten, Sam Weaver, Sander De Neve [X], Sebastian Lopez, Shane Hou, Shaun Berry, Snap Snerfing, Sparsh, Steve Babigian, Steve K, Tavacizade Toruno?lu, Tess Avitabile, Timothy Pendergraft [X], Tolga Evcimen, Tony Ellard, Tony Mobily, Tyler Stroud, vijay kumar, vincent daubry, Vincent Huang, walay, wangdebing, Wen, Yannick Bétemps [X], yudho ahmad diponegoro, Zoltan Altfatter
            Votes:
            351 Vote for this issue
            Watchers:
            278 Start watching this issue

              Created:
              Updated:
              Resolved: