15

I have this :

{
  "_id" : ObjectId("4fb4fd04b748611ca8da0d48"),
  "Name" : "Categories",
  "categories" : [{
      "_id" : ObjectId("4fb4fd04b748611ca8da0d46"),
      "name" : "SubCategory",
      "sub-categories" : [{
          "_id" : ObjectId("4fb4fd04b748611ca8da0d47"),
          "name" : "SubSubCategory",
          "standards" : []
        }]
    }]
}

I would like to add a new SubCategory using the C# driver. Is there an optimal way to do this?

3 Answers 3

23

You can do this using FindOneAndUpdateAsync and positional operator

public async Task Add(string productId, string categoryId, SubCategory newSubCategory)
{
    var filter = Builders<Product>.Filter.And(
         Builders<Product>.Filter.Where(x => x.Id == productId), 
         Builders<Product>.Filter.Eq("Categories.Id", categoryId));
    var update = Builders<Product>.Update.Push("Categories.$.SubCategories", newSubCategory);
    await collection.FindOneAndUpdateAsync(filter, update);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! But what if array doesn't exist yet? Is there a way to create it here and then push?
5

You can use the positional operator using Linq expressions too:

public async Task Add(string productId, string categoryId, SubCategory newSubCategory)
{
    var filter = Builders<Product>.Filter.And(
         Builders<Product>.Filter.Where(x => x.Id == productId), 
         Builders<Product>.Filter.ElemMatch(x => x.Categories, c => c.Id == categoryId));
    var update = Builders<Product>.Update.Push(x => x.Categories[-1].SubCategories, newSubCategory);
    await collection.FindOneAndUpdateAsync(filter, update);
}

And get yourself free of using hard-coded property names inside strings.

3 Comments

what '-1' means ?
It's the way to use it, in c# mongodb driver: stackoverflow.com/questions/42396877/…
0

Adding an example for my case. Did work but without the dollar sign when entering inside an array:

public async Task AddCustomMetadata()
       {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("EntityCustomMetadataFieldId", "5bf81296-feda-6447-b45a-08d5cb91211c");
            var filter = Builders<BsonDocument>.Filter.Eq("_id", "6c7bb4a5-d7cc-4a8d-aa92-b0c89ea0f7fe");
            var update = Builders<BsonDocument>.Update.Push("CustomMetadata.Fields", dic);
            await _context.BsonAssets.FindOneAndUpdateAsync(filter, update);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.