2

I am using Java Mongo driver for the DB interaction. I have regular updates to be performed on the DB rows and the object that is quite nested. Something like this :

MyObject:

{
    _id: dbGeneratedId,
    myId: "A String ID that i created",
    myTime: "new Date()",
    myList: 
        [
        {
            myString: "abcdefghij",
            myInteger: 9000
        },
        {
            myString: "qwertyasdf",
            myInteger: 9001
        },
        {
            myString: "loremipsum",
            myInteger: 9002
        }
    ]
}

Each update involves either adding a new List item under myList or appending some string to the myString object in each of the List item. I found a lot of references for writing/finding items and none for updating items in a nested object. Can someone help me with this.

Edit 1: It would also be helpful if someone points out how to get one of the List items based on a myInteger search

PS: new to mongo thro Java, excuse my ignorance

Community
  • 1
  • 1
Some guy
  • 1,210
  • 1
  • 17
  • 39

1 Answers1

2

You can insert new list item using the $push operator.

You can run the following command on the shell to add new list item.

db.myObject.update({"myId" : "A String ID that i created"},{$push:{myList: {myString:"new string", myInteger:9003}}})

You can add list item using Java Driver as follows.

        DBCollection coll = db.getCollection("myObject");
        DBObject query = new BasicDBObject("myId", "A String ID that i created");

        DBObject listItem = new BasicDBObject();
        listItem.put("myString", "my new string");
        listItem.put("myInteger", 9003);

        DBObject updateObj = new BasicDBObject("myList", listItem);

        coll.update(query, new BasicDBObject("$push", updateObj));

You can get single element as follows on the shell.

db.myObject.find({"myList.myInteger" : 9003}, {"myList.$" : 1})

From Java Driver you can run same code as follows :

DBCursor cur = coll.find(new BasicDBObject("myList.myInteger", 9003), new BasicDBObject("myList.$", 1));

You can remove object as follows :

db.nested.update({"myId" : "A String ID that i created"},{$pull:{myList: {myString:"new string", myInteger:9003}}})

In Java you can do it as follows :

    DBCollection coll = db.getCollection("myObject");
    DBObject query = new BasicDBObject("myId", "A String ID that i created");

    DBObject listItem = new BasicDBObject();
    listItem.put("myString", "my new string");
    listItem.put("myInteger", 9003);

    DBObject updateObj = new BasicDBObject("myList", listItem);

    coll.update(query, new BasicDBObject("$pull", updateObj));

PS : Currently it is not possible to update all items in an array. There is an open issue on the Jira. You can check it from JIRA-1243

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • This did help!. Also, do you know how to update/append a value already in the list?. Say, i need to update the `myString` value of `loremipsum` to `loremipsumfoobar`, how do i do that? Any help is greatly appreciated – Some guy Sep 04 '13 at 10:54
  • 1
    I have updated my answer. Currently it is not possible to update all array items. There is an open issue on Jira. – Parvin Gasimzade Sep 04 '13 at 10:56
  • On a side note, could you share a snippet showing how to delete one of the objects from `myList`. – Some guy Sep 04 '13 at 11:29
  • 1
    Updated my answer. Check it again. – Parvin Gasimzade Sep 04 '13 at 12:22