Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate embedded document details in MongoDB
    primarykey
    data
    text
    <p>In MongoDB, if I have a document structure as follows:</p> <pre><code>{ "_id" : { "$binary" : "jchPoPd7PUS1w+sR7is23w==", "$type" : "03" }, "companies" : [ { "_id" : { "$binary" : "jchPoPd7PUS1w+sR7is23w==", "$type" : "03" }, "name" : "Google" }, { "_id" : { "$binary" : "jchPoPd7PUS1w+sR7is23w==", "$type" : "03" }, "name" : "Greenfin" }, { "_id" : { "$binary" : "jchPoPd7PUS1w+sR7is23w==", "$type" : "03" }, "name" : "Zynet" } ], "firstname" : "Peter", "surname" : "Smith" } </code></pre> <p>(i.e. a <code>Person</code> document with a <code>Companies</code> array embedded within the person document), then how do I update ALL occurrences of a specific Company (targetting via the company _id) with a single query+update?</p> <p>I have tried the following:</p> <pre><code>MongoCollection personCollection = mdb.GetCollection("person"); BsonBinaryData bin = new BsonBinaryData(new Guid("0AE91D6B-A8FA-4D0D-A94A-91D6AC9EE343")); QueryComplete query = Query.EQ("companies._id", bin); var update = Update.Set("companies.name", "GreenfinNewName"); SafeModeResult result = personCollection.Update(query, update, UpdateFlags.Multi); </code></pre> <p>but it doesn't work. I guess my question boils down to two questions: how do I target the embedded companies in the initial query, and then how do I set the new company name in the Set statement. Note: In this example I have chosen to "denormalise" the company data, and embed it in each person document, so there may be several person documents with the same company id and name. Thanks very much.</p> <p>UPDATE: Using Bugai13's technique I've got closer. This works:</p> <pre><code>MongoCollection personCollection = mdb.GetCollection("person"); QueryComplete query = Query.EQ("companies.name", "Bluefin"); var update = Update.Set("companies.$.companynotes", "companynotes update via name worked"); SafeModeResult result = personCollection.Update(query, update, UpdateFlags.Multi, SafeMode.True); </code></pre> <p>But this doesn't work:</p> <pre><code>MongoCollection personCollection = mdb.GetCollection("person"); BsonBinaryData bin = new BsonBinaryData(new Guid("0AE91D6B-A8FA-4D0D-A94A-91D6AC9EE343")); Builders.QueryComplete query = Query.EQ("companies._id", bin); var update = Update.Set("companies.$.companynotes", "companynotes update via id worked"); SafeModeResult result = personCollection.Update(query, update, UpdateFlags.Multi, SafeMode.True); </code></pre> <p>So, I can't yet update using the primary key, which is what I need to do...</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