Update/Delete a sub document in mongodb using C# driver

前端 未结 3 1900
失恋的感觉
失恋的感觉 2021-01-03 07:34

I have 2 classes:

public class Vote
{
    public string VoteId { get; set; }
    public string Question { get; set; }
    public List Answe         


        
相关标签:
3条回答
  • 2021-01-03 08:26
    // Example function for update like count add like user  using c#    
    public PostModel LikeComment(LikeModel like)
    {
        PostModel post = new PostModel();
    
        _client = new MongoClient();
        _database = _client.GetDatabase("post");
        var collection = _database.GetCollection<PostModel>("post");
    
        var _filter = Builders<PostModel>.Filter.And(
        Builders<PostModel>.Filter.Where(x => x.PostId == like.PostId),
        Builders<PostModel>.Filter.Eq("Comments.CommentId", like.CommentId));
    
        var _currentLike = collection.Find(Builders<PostModel>.Filter.Eq("PostId", like.PostId)).FirstOrDefault().Comments.Find(f => f.CommentId == like.CommentId).Like;
    
        var update = Builders<PostModel>.Update.Set("Comments.$.Like", _currentLike + 1);
        collection.FindOneAndUpdate(_filter, update);
    
        var addUser = Builders<PostModel>.Update.Push("Comments.$.LikeUsers", like.UserId);
        collection.FindOneAndUpdate(_filter, addUser);
    
        var _findResult = collection.Find(_filter).FirstOrDefault();
    
        return _findResult;
    }
    
    0 讨论(0)
  • 2021-01-03 08:35
    //Delete comment
    public PostModel delcomment(int postId, int commentId)
    {
        _client = new MongoClient();
        _database = _client.GetDatabase("post");
        var collection = _database.GetCollection<PostModel>("post");
    
        var filter = Builders<PostModel>.Filter.Eq("PostId", postId);
    
        var update = Builders<PostModel>.Update.PullFilter("Comments",
        Builders<Comments>.Filter.Eq("CommentId", commentId));
    
        collection.FindOneAndUpdate(filter, update);
    
        var _findResult = collection.Find(filter).FirstOrDefault();
        return _findResult;
    }
    
    0 讨论(0)
  • 2021-01-03 08:39

    To update subdocument you can use this:

    var update = Update.Set("AnswerList.$.OptionName", "new").Set("AnswerList.$.VoteCount", 5);
    collection.Update(Query.And(Query.EQ("_id", new BsonObjectId("50f3c313f216ff18c01d1eb0")), Query.EQ("AnswerList.OptionId", "1")), update);
    

    profiler:

    "query" : { "_id" : ObjectId("50f3c313f216ff18c01d1eb0"), "AnswerList.OptionId" : "1" },
    "updateobj" : { "$set" : { "AnswerList.$.OptionName" : "new", "AnswerList.$.VoteCount" : 5 } }
    

    And to remove:

    var pull = Update<Vote>.Pull(x => x.AnswerList, builder => builder.EQ(q => q.OptionId, "2"));
    collection.Update(Query.And(Query.EQ("_id", new BsonObjectId("50f3c313f216ff18c01d1eb0")), Query.EQ("AnswerList.OptionId", "2")), pull);
    

    profiler:

    "query" : { "_id" : ObjectId("50f3c313f216ff18c01d1eb0"), "AnswerList.OptionId" : "2" },
    "updateobj" : { "$pull" : { "AnswerList" : { "OptionId" : "2" } } }
    

    Another way is to update parent document with modified child collection.

    0 讨论(0)
提交回复
热议问题