Update or replace an embedded document in MongoDB collection

前端 未结 1 1850
天命终不由人
天命终不由人 2020-12-22 08:43

I have following data structure:

db.objects
{
    \"_id\" : 1,
    \"name\" : \"object name\",
    \"type\" : \"rectangle\",
    \"area\" : {
        \"posit         


        
相关标签:
1条回答
  • 2020-12-22 09:37

    This is a very simple use of the $set operator. So essentially the update form is:

    db.objects.update(
        { "_id": 1 },
        {
            "$set": {
                "area": { /* all of your object properties */ },
                "dimension": { /* all of your object properties */ }
            }
        }
    )
    

    Alternately you do them separately:

    db.objects.update(
        { "_id": 1 },
        {
            "$set": {
                "area.position_x1": 1,
                "area.position_y1": 1,
                "dimension.width": 1 
            }
        }
    )
    

    According to you actual needs.

    Or as C# type code with a Builder:

    var query = Query.EQ("_id",1);
    
    var update = Update.Set("area.position_x1", 1)
        .Set("area.position_y1", 1)
        .Set("dimension.width", 1);
    
    MongoCollection<Class>.update(query, update);
    
    0 讨论(0)
提交回复
热议问题