2

I am a beginner in MongoDB. Please see my models below.

public class Technology
{
    public Technology()
    {
        ProductGroups = new List<ProductGroup>();
    }

    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }

    public string Name { get; set; }

    public IEnumerable<ProductGroup> ProductGroups { get; set; }
}

public class ProductGroup
{

    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }

    public string Name { get; set; }

}

Now the data shows like below.

enter image description here

I am try to add the ProductGroup ( it's a BsonDocument Collection) collection in Technology.

3 Answers 3

3
  1. Use generic types where you can. Because this code parent["ProductGroups"] is dangerous place for any refactoring.
  2. Your task can be done in one query

var productGroup = new ProductGroup { Id = ObjectId.GenerateNewId(), Name = model.Name };
var collection = database.GetCollection<Technology>("Technology");
var update = Builders<Technology>.Update.AddToSet(x => x.ProductGroups, productGroup);
await collection.FindOneAndUpdateAsync(x => x.Id == model._id, update);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your valuable reply. But i can't get this (FindOneAndUpdateAsync) method in C# intellisence
How do you get collection?
2

Change your Model of Technology as

[BsonElementAttribute("productgroups")]
public IList<ProductGroup> ProductGroups{ get; set; }

Then,

var productGroup = new BsonDocument().Add("_id", productGroup_id).Add("Name", name);

var technologies = database.GetCollection("technology");
var technology = technologies.FindOneById(ObjectId.Parse(technology_id));

technology["productgroups"] = new BsonArray().Add(BsonValue.Create(productGroup));

technologies.Save(technology);

2 Comments

Thank you for your valuable reply. I try to do this method. But '.FindOneById' is not getting visual studio intellisence. I am using v2.0.1.27
@RageshPuthiyedath If you have MongoDB.Driver.dll referenced, then check api.mongodb.org/csharp/1.0/html/…. If this doesn't work you can check any other option using VS intellisense.
1

@CodingDefined I change my code as per the v2.0.1.27

Please see my code below. Thank you very much for your help.

var productGroup = new BsonDocument()
                      .Add("_id", ObjectId.GenerateNewId())
                      .Add("Name", model.Name);

BsonDocument parent = null;

var _parent = Collection.FindOneByIdAs(typeof(BsonDocument), model._id);

if (_parent != null)
{

   parent = _parent.ToBsonDocument();

   parent["ProductGroups"] = new BsonArray().Add(BsonValue.Create(productGroup));

   Collection.Save(parent);

}

Please make sure, the new child record is not clearing the existing records

parent["ProductGroups"] = parent["ProductGroups"].AsBsonArray.Add(productGroup);

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.