Araê Souza
Araê Souza

Reputation: 79

Insert new elements into a documento using mongolite

I am trying to update a document in mongo. The objective is to insert new elements inside the same document, keeping the original structure. However, I'm having some problems with it.

The mongo is structure as follows:

[{
  "_id": {
    "$oid": "648a061e7860d555a70d436a"
  },
  "callbackUrl": "https://teste",
  "factors": {
    "q1": 50,
    "q2": 75.5,
    "q3": 30.2,
    "q4": 90.7,
    "q5": 65.3,
    "q6": 42.1,
    "q7": 80.9,
    "q8": 55.6,
    "q9": 70,
    "q10": 15.8
  },
  "users": [
    {
      "user_id": "user1",
      "responses": [
        {"question_id": "q1","correct": true},
        {"question_id": "q2","correct": false},
        {"question_id": "q3","correct": true},
        {"question_id": "q4","correct": true},
        {"question_id": "q5","correct": false}
      ]
    },
    {
      "user_id": "user2",
      "responses": [
        {"question_id": "q1","correct": true},
        {"question_id": "q2","correct": true},
        {"question_id": "q6","correct": false},
        {"question_id": "q7","correct": true},
        {"question_id": "q8","correct": true},
        {"question_id": "q9","correct": false},
        {"question_id": "q10","correct": false}
      ]
    }
  ],
  "TransactionId": "28eba39f-a772-447a-9916-fd81ffb9970a",
  "state": "created"
}]

Now I want to add a new user, and I have a object named data$users (a json) that is structured as follows:

[
  {
    "user_id":"user20",
    "responses":[
      {"question_id":"q1","correct":true},
      {"question_id":"q2","correct":false},
      {"question_id":"q3","correct":true},
      {"question_id":"q4","correct":true},
      {"question_id":"q5","correct":false}
    ]
  }
]

I'd like the final result to be this:

[{
  "_id": {
    "$oid": "648a061e7860d555a70d436a"
  },
  "callbackUrl": "https://teste",
  "factors": {
    "q1": 50,
    "q2": 75.5,
    "q3": 30.2,
    "q4": 90.7,
    "q5": 65.3,
    "q6": 42.1,
    "q7": 80.9,
    "q8": 55.6,
    "q9": 70,
    "q10": 15.8
  },
  "users": [
    {
      "user_id": "user1",
      "responses": [
        {"question_id": "q1","correct": true},
        {"question_id": "q2","correct": false},
        {"question_id": "q3","correct": true},
        {"question_id": "q4","correct": true},
        {"question_id": "q5","correct": false}
      ]
    },
    {
      "user_id": "user2",
      "responses": [
        {"question_id": "q1","correct": true},
        {"question_id": "q2","correct": true},
        {"question_id": "q6","correct": false},
        {"question_id": "q7","correct": true},
        {"question_id": "q8","correct": true},
        {"question_id": "q9","correct": false},
        {"question_id": "q10","correct": false}
      ]
    },
    {
      "user_id":"user20",
      "responses":[
        {"question_id":"q1","correct":true},
        {"question_id":"q2","correct":false},
        {"question_id":"q3","correct":true},
        {"question_id":"q4","correct":true},
        {"question_id":"q5","correct":false}
      ]
    }
  ],
  "TransactionId": "28eba39f-a772-447a-9916-fd81ffb9970a",
  "state": "created"
}]

I've tried several things. This would be obvious to me, but id deletes the first 2 users:

con$update(
    query = glue::glue('{{"TransactionId": "{id}"}}'),
    update = glue::glue('{{"$set": {{"users": {jsonlite::toJSON(data$users, auto_unbox = TRUE)}}}}}') ,
    upsert = TRUE
  )

I've also tried this code, that unfortunately added a new array inside users, and inside this array it inserted a new array with a new user.

con$update(
    query = glue::glue('{{"TransactionId": "{id}"}}'),
    update = glue::glue('{{"$push": {{"users": {jsonlite::toJSON(data$users, auto_unbox = TRUE)}}}}}') ,
    upsert = TRUE
  )

What should I do?

Upvotes: 1

Views: 30

Answers (1)

Araê Souza
Araê Souza

Reputation: 79

After further review I noticed that i could push into users considering each new entry. The following code worked:

con$update(
    query = glue::glue('{{"TransactionId": "{id}"}}'),
    update = glue::glue('{{"$push": {{"users": {{"$each":{jsonlite::toJSON(data$users, auto_unbox = TRUE)}}}}}}}'),
    upsert = TRUE
  )

Upvotes: 0

Related Questions