0

The userlist collection contains documents of the following format.

{
        "_id" : ObjectId("5381d32ce72cc794166eede2"),
        "name" : "Haseeb",
        "password" : "dgkhan",
        "email" : "[email protected]",
        "address" : "237 D, Faisal Town , Lahore",
        "phone" : "5162806"
}

I intend to add another member in the existing document such that the resultant document looks like this.

{
            "_id" : ObjectId("5381d32ce72cc794166eede2"),
            "name" : "Haseeb",
            "password" : "dgkhan",
            "email" : "[email protected]",
            "address" : "237 D, Faisal Town , Lahore",
            "phone" : "5162806",
            "purchases" : [{
                "itemID": xyz,
                "quantity": 142
            },
            {
                "itemID": kjh,
                "quantity": 987
            }
            }]

}

For this I have written the following mongoskin query, but it is not performing any updates.

db.collection('userlist').update(
                {_id:req.session._id},
                 {
                    '$push': { purchases: {
                        itemID: item.ID,
                        quantity: item.quantity
                    }
                }
            }, function(err) {
                if (err) throw err;
                    console.log('Updated!');
            });
        }

Value of req.session._id = 5381d32ce72cc794166eede2 i.e a valid _id field of a docuemnt in my collection whereas item.ID and item.quantity are also valid strings. Any help would be really appreciated.

3 Answers 3

2

I'm more familiar with Mongoose, but looking at the docs you could probably either use:

collection.updateById(req.session._id, ...)

or

collection.update({_id: toObjectID(req.session_id)}, ...)
Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to convert my id into a proper object Id hex string. toObjectID(req.session_id) fixed it
Yep! that's what I was referring to. Sorry I didn't make it more clear.
0

I think you should be using $set instead of $push.

A good resource for this specific question is: http://docs.mongodb.org/manual/reference/method/db.collection.update/#db.collection.update

1 Comment

No, $set overrites the existing value which is not what I intend to do here. I want to add a purchase document to the existing set of purchases.
0

Something like this. You need to include the $each syntax.

db.collection('userlist').update(
                {_id:req.session._id},
                 {
                    $push { purchases: { $each: {
                        itemID: item.ID,
                        quantity: item.quantity
                        }
                    }
                }
            }, function(err) {
                if (err) throw err;
                    console.log('Updated!');
            });
    }

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.