Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating a specific key/value inside of an array field with MongoDB
    primarykey
    data
    text
    <p>As a preface, I've been working with MongoDB for about a week now, so this may turn out to be a pretty simple answer.</p> <p>I have data already stored in my collection, we will call this collection <code>content</code>, as it contains articles, news, etc. Each of these <code>articles</code> contains another array called <code>author</code> which has all of the author's information (Address, Phone, Title, etc). </p> <p><strong>The Goal</strong> - I am trying to create a query that will update the author's address on every article that the specific author exists in, and only the specified author block (not others that exist within the array). </p> <p>Sort of a "Global Update" to a specific article that affects his/her information on every piece of content that exists. </p> <p>Here is an example of what the <code>content</code> with the <code>author</code> looks like.</p> <pre><code>{ "_id" : ObjectId("4c1a5a948ead0e4d09010000"), "authors" : [ { "user_id" : null, "slug" : "joe-somebody", "display_name" : "Joe Somebody", "display_title" : "Contributing Writer", "display_company_name" : null, "email" : null, "phone" : null, "fax" : null, "address" : null, "address2" : null, "city" : null, "state" : null, "zip" : null, "country" : null, "image" : null, "url" : null, "blurb" : null }, { "user_id" : null, "slug" : "jane-somebody", "display_name" : "Jane Somebody", "display_title" : "Editor", "display_company_name" : null, "email" : null, "phone" : null, "fax" : null, "address" : null, "address2" : null, "city" : null, "state" : null, "zip" : null, "country" : null, "image" : null, "url" : null, "blurb" : null }, ], "tags" : [ "tag1", "tag2", "tag3" ], "title" : "Title of the Article" } </code></pre> <p>I can find every article that this author has created by running the following command:</p> <pre><code>db.content.find({authors: {$elemMatch: {slug: 'joe-somebody'}}}); </code></pre> <p>So theoretically I should be able to update the <code>authors</code> record for the slug <code>joe-somebody</code> but not <code>jane-somebody</code> (the 2nd author), I am just unsure exactly how you reach in and update every record for that author.</p> <p>I thought I was on the right track, and here's what I've tried.</p> <pre><code>b.content.update( {authors: {$elemMatch: {slug: 'joe-somebody'} } }, {$set: {address: '1234 Avenue Rd.'} }, false, true ); </code></pre> <p>I just believe there's something I am missing in the $set statement to specify the correct author and point inside of the correct array. Any ideas?</p> <p><code>**Update**</code></p> <p>I've also tried this now:</p> <pre><code>b.content.update( {authors: {$elemMatch: {slug: 'joe-somebody'} } }, {$set: {'authors.$.address': '1234 Avenue Rd.'} }, false, true ); </code></pre> <h2>Solution:</h2> <p>This is what finally worked for me!</p> <pre><code>db.content.update({'authors.slug':'joe-somebody'},{$set:{'authors.$.address':'Address That I wanted'}},false,true); </code></pre> <p>It updates all the records properly, thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload