skip to Main Content

I have a document that looks something like this

{

    "_id":{
        "$oid":"id"
    },
    "side1":[
        {
            "username":"test1",
            "id":"id1",
            
        },
        {
            "username":"test2",
            "id":"id2",
            
        },
        {
            "username":"test3",
            "id":"id3",
            
        }
       
    ],
    "side2":[
        {
            "username":"test4",
            "id":"id4",
            
        },
        {
            "username":"test5",
            "id":"id5",
            
        },
        {
            "username":"test6",
            "id":"id6",
            
        }
        
    ],


}

I want to be able to search and update one of the sides, for example, if I searched with username for side1 and that username would be there then I would be able to $set other fields for the object with this username. Something like: Search side1 username test1: $set result.id: "43242342" this would set the id of the object with the username of test1 to 43242342. I am not sure on how I would go about doing this, I have tried using $elemMatch but that didn’t bring any results.

Test.findOne({ id: id }, 
                
                { side1: { $elemMatch: {username: username} } }, function (err, result) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(result)
                }
            });

2

Answers


  1. Could you do something like this?

    function addProperty(id, side, username, property, value) {
      const query = {
        _id: {
          $oid: id,
        },
      };
    
      const update = {
        $push: {
          [side]: {
            username: username,
            [property]: value,
          },
        },
      };
    
      const options = {
        upsert: true,
      };
    
      db.collection("Test").updateOne(query, update, options);
    }
    
    Login or Signup to reply.
  2. I’m not exactly sure how you want to update the document, but perhaps this is what you are looking for?

    db.collection.update({
      "_id": ObjectId("000000000000000000000001"),
      "side1.username": "test1"
    },
    {
      "$set": {
        "side1.$.id": "43242342"
      }
    })
    

    Try it on mongoplayground.net.

    Example updated document:

    [
      {
        "_id": ObjectId("000000000000000000000001"),
        "side1": [
          {
            "id": "43242342",
            "username": "test1"
          },
          {
            "id": "id2",
            "username": "test2"
          },
          {
            "id": "id3",
            "username": "test3"
          }
        ],
        "side2": [
          {
            "id": "id4",
            "username": "test4"
          },
          {
            "id": "id5",
            "username": "test5"
          },
          {
            "id": "id6",
            "username": "test6"
          }
        ]
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search