0
{
    "_id" : "TestData123",
    "data" : [ 
        {
            "ch" : "test1",
            "prod" : [ 
                {
                    "sid" : "1",
                    "value" : " TV"
                }, 
                {
                    "sid" : "2",
                    "value" : "amazon",
                }, 
                {
                    "sid" : "3",
                    "value" : "ebay",
                }, 
                {
                    "sid" : "4",
                    "value" : "otherthing",
                }
            ]
        }, 
        {
            "ch" : "test2",
            "prod" : [ 
                {
                    "sid" : "6",
                    "value" : "TV",
                }, 
                {
                    "sid" : "7",
                    "value" : "amazon",
                }, 
                {
                    "sid" : "8",
                    "value" : "ebay",
                }, 
                {
                    "sid" : "9",
                    "value" : "otherthing",
                }
            ]
        }
    ]
}

I have a mongoDB collection in the above format. I need to remove/empty the array 'prod' in ch:test1 and ch:test2. Also please suggest the mongo query for adding the elements to these as well.

2
  • You want to remove some element from the array or empty the array? Commented Apr 7, 2015 at 17:46
  • I want to empty the array and then add new elements to it. Commented Apr 7, 2015 at 17:49

2 Answers 2

1

I've cleaned up and formatted your BSON object:

{
   "_id":"TestData123",
   "data":[
      {
         "ch":"test1",
         "prod":[
            {
               "sid":"1",
               "value":" TV"
            },
            {
               "sid":"2",
               "value":"amazon"
            },
            {
               "sid":"3",
               "value":"ebay"
            },
            {
               "sid":"4",
               "value":"otherthing"
            }
         ]
      },
      {
         "ch":"test2",
         "prod":[
            {
               "sid":"6",
               "value":"TV"
            },
            {
               "sid":"7",
               "value":"amazon"
            },
            {
               "sid":"8",
               "value":"ebay"
            },
            {
               "sid":"9",
               "value":"otherthing"
            }
         ]
      }
   ]
}

Here's another stackoverflow post with the same question:

https://stackoverflow.com/a/6327971/980423

You're looking for the $unset operator.

  db.tmp.update({"_id":"TestData123", "data.ch": "test1"}, {$unset: {"data.$.prod": 1}});

It's also tricky because it's in an array.

  db.tmp.update({"_id":"TestData123", "data.ch": "test1"}, {$set: {"data.$.prod": <some array>}});

Will set it again.

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

Comments

0

I'm not sure what language you're using but here is how I would do it in C#. I would first get the document with the id.

var dbcollection = db.GetCollection("");
var document = dbcollection.FindOneById(ObjectId.Parse(id));
var query = new QueryDocument { { "_id", ObjectId.Parse(id) } };

Then I would update the document with the modified version.

BsonArray array = new BsonArray(); //leave empty or add same structure but empty fields
UpdateBuilder update = new UpdateBuilder();
update.Set("data.0.prod", array );
var result = dbcollection.Update(query, update);

This will empty the array. I haven't emptied an array, I've just updated one but I think this will work.

This also might work I haven't tested it out yet.

dbcollection.Remove(Query.EQ("_id", ObjectId.Parse(id)), document["data"][0]["prod"] );

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.