Trouble Parsing JSON Data

Hi all - I’m new to using Mongo so I apologize for anything that looks dumb. I handle updating my records with a Realms webhook that works perfectly, EXCEPT for when I try to update the arrays I store inside my DB objects.

Here is an example of a JSON post I am making to this webhook:

    "objectID": "5fa0bdb636a703546ee07c52",
    "id": "Joe",
    "value": {
        "id": "test",
        "dob": "20-20",
        "purchDate": "2020-09-02",
        "purchFrom": "2020-09-02",
        "location": "a",
        "category": "a",
        "breed": "a",
        "weight": "a",
        "damID": "a",
        "sireID": "a",
        "generalList": [
                "genDate": "10-10",
                "genComment": "comment1"
                "genDate": "20-20",
                "genComment": "comment2"
        "performanceList": [
                "perfDateGain": "10-10",
                "perfWeight": "123",
                "perfBCS": "123",
                "perfChange": "123",
                "perfRate": "123",
                "perfDateBreed": "10-10",
                "perfEval": "123",
                "perfCommentBreed": "asd"
        "healthList": [
                "healthDate": "10-10",
                "healthIssue": "asd",
                "healthAction": "asd",
                "healthDosage": "123",
                "healthComment": "asd"

And here is the function code I’m using to update the DB:

Like I said, it has something to do with the way I am accessing the index of my array inside of my object, because everything works fine except for that part. Is there a special way to access array elements inside objects? Thanks!

Hi @Joewangatang and welcome in the MongoDB Community :muscle: !

I think what you are trying to do here is to set all the values from your value field into your document in MongoDB with _id == "the value from objectID field".

If that is correct then I think you can get away from this with a simple function like this:

exports = function(payload, response) {
    const body = EJSON.parse(payload.body.text());
    const id = BSON.ObjectId(body.objectID);
    const coll ="mongodb-atlas").db("test").collection("coll");
    return coll.updateOne({'_id': id},{'$set': body.value});

Here is my test.

I inserted this doc:

MongoDB Enterprise Free-shard-0:PRIMARY> db.coll.insert({name:"Maxime"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise Free-shard-0:PRIMARY> db.coll.findOne()
{ "_id" : ObjectId("5ff7b41bc922979b2f673bcf"), "name" : "Maxime" }

Then I sent my cURL command:

curl \
-H "Content-Type: application/json" \
-d '{"objectID":"5ff7b41bc922979b2f673bcf", "value": {"name":"Maxime Beugnet", "age": 32}}' \

And then my document was like this:

MongoDB Enterprise Free-shard-0:PRIMARY> db.coll.findOne()
	"_id" : ObjectId("5ff7b41bc922979b2f673bcf"),
	"name" : "Maxime Beugnet",
	"age" : 32

As you can see above, my field name was updated and the new field age was added correctly.

I hope I understood what you were trying to do correctly.


1 Like

Hey Maxime,

Thank you! This is exactly what I was looking for, except your function replaced the whole document. I modified it to only change the “value” field of my record, as that’s where all the information is stored:

return coll.updateOne({'_id': id},{'$set': {"value":body.value}});


1 Like

Oops :slight_smile: ! But I’m glad I have put you the right direction.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.