0

I want to update the single document in collection with the guid as filter and update value is cityType. Every guid has different citytype here i have used 3 types it may be more. So please give a right implementation using c# code.

Models:

public class Country
    {
        [BsonId]
        public ObjectId Id { get; set; }
        public int CountryId {get; set; }
        public IEnumerable<States> States { get; set; }
    }
    
    public class States
    {
        public Guid Guid { get; set; }
        public CityType CityType { get; set; }
    }
    
    Enum CityType
    {
      Unknown = 0,
      Rural = 1,
      Urban = 2
    }

Existing Collection:

{
    "_id": ObjectId("6903ea4d2df0c5659334e763"),
    "CountryId": 200,
    "States": [
      {
        "Guid": "AFCC4BE7-7585-5E46-A639-52F0537895D8",
        "CityType": 0,
      },
      {
        "Guid": "208FB603-08C7-46D9-B0C0-7AF4F691A96D",
        "CityType": 0,
      }
    }

Input:

List<States>()
    {
        new States()
        {
           Guid = "AFCC4BE7-7585-5E46-A639-52F0537895D8",
           CityType = CityType.Rural
        },
        new States()
        {
           Guid = "208FB603-08C7-46D9-B0C0-7AF4F691A96D",
           CityType = CityType.Urban
        }
    }

Expected:

{
    "_id": ObjectId("6903ea4d2df0c5659334e763"),
    "CountryId": 200,
    "States": [
      {
        "Guid": "AFCC4BE7-7585-5E46-A639-52F0537895D8",
        "CityType": 1,
      },
      {
        "Guid": "208FB603-08C7-46D9-B0C0-7AF4F691A96D",
        "CityType": 2,
      }
    }

This is the method I have tried:

public async Task<bool> UpdateType(int countryId, IEnumerable<States> states)
      {
         var collection = connectionFactory.GetCollection<Country>(collectionName);

         var cityTypes = states.Select(x => x.CityType);
         var filter = Builders<Country>.Filter.Empty;
         var update = Builders<Country>.Update.Set("States.$[edit].CityType", cityTypes);

         var arrayFilters = new List<ArrayFilterDefinition>();

         foreach (var state in states)
         {
            ArrayFilterDefinition<Country> optionsFilter = new BsonDocument("state.Guid", new BsonDocument("$eq", state.Guid));
            arrayFilters.Add(optionsFilter);
         }

         var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };

         var result = await collection.UpdateOneAsync(filter, update, updateOptions);
         return result;
      }

hope all details I have added here. Thanks in advance.

1
  • You can get some help from here Commented Nov 28, 2022 at 14:16

1 Answer 1

1

You don't have to loop through it:

Let's say you have a Class1 like this:

class Question : AuditableEntity {
    public string Text { get; set; }
    public List<string> Tags { get; set; } = new List<string>();

so you just say:

await collection.UpdateOneAsync(
            someFilter,
            Builders<Class1>.Update
                .Set(f => f.Text, request.Question.Text)
                .Set(f => f.Tags, request.Question.Tags));
Sign up to request clarification or add additional context in comments.

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.