1

I want to update the "user"-array of the following JSON-document which is saved in a MongoDB Database (programming language C++)

{
        "_id" : ObjectId("5ac4beacc0e2a512e6377d43"),
        "document" : "test",
        "user" : [
            {
                "email" : "[email protected]",
                "username" : "Anton Held",
                "job" : "",
            },
            {
                "email" : "[email protected]",
                "username" : "Benny Bill",
                "job" : "IT-Officer",
            },
            {
                "email" : "[email protected]",
                "username" : "Conny Cenn",
                "job" : "",
            },
        ]
    }

Therefore I am using this command to get this specific document:

collection.update_one(document{} << "document" << "test"
                                     << finalize,
                      document{} << "$set" << open_document <<
                                 (HOW TO DEAL?!) close_document << finalize);

I know that it is possible to update a document with the "update_one"-method (or similar methods like "replace_one", "find_one_and_replace").

But I don't know how to deal with this methods to update as example the username of the existing user with the "email"-element="[email protected]" or how I could add a user to the existing array "user".

Please help me :-)

1
  • Okay that works with this command, but i am not able to convert this to C++: db.getCollection('testcollection').update({"document":"test", "user.email":"[email protected]"}, {"$set": {"user.$.username":"New Username"}}) Please take a look at my last example :collection.update_one(document{} << "document" << "test" << finalize, document{} << "$set" << open_document << (HOW TO DEAL?!) close_document << finalize); Commented Apr 8, 2018 at 10:26

3 Answers 3

2

So you question boils down to how to convert this mongo command to C++ code:

db.getCollection("testcollection").update(
   {"document":"test", "user.email":"[email protected]"},
   {"$set": {"user.$.username":"New Username"}}
)

Here's how it can look like. Notice the pattern:

  • Output keys and values in pairs
  • Use open_document and close_document for nested { and }

Sample result:

collection.update_one(
  document{} << "document" << "test"
             << "user.email" << "[email protected]"
             << finalize,
  document{} << "$set"
             << open_document
                 << "user.$.username" << "New Username"
             << close_document << finalize
);
Sign up to request clarification or add additional context in comments.

Comments

0

As the documentation says, you can do as follow to add a new user:

db.getCollection("testcollection").update(
   { "document": "test"},
   { "$push": {
         "user": {
             "username":"A new user in town ;)",
             "email": "[email protected]"
             "job": "A cool job for a cool user"
         }
     }
   }
)

In mongodbcxx:

As @rustyx mentioned you can simply change {} to open/close document and to get:

collection.update_one(
  document{}
      << "document" << "test"
      << finalize,

  document{}
      << "$push" << open_document
          << "user" << open_document
              << "username" << "A new user in town ;)"
              << "email"    << "[email protected]"
              << "job"      << "A cool job for a cool user"
          << close_document
      << close_document
      << finalize
);

BTW, I would recommend to replace "user" array name to "users", because it's users array and not a user array.

Comments

-1

Okay that works with this command, but i am not able to convert this to C++:

db.getCollection('testcollection').update({"document":"test", "user.email":"[email protected]"}, {"$set": {"user.$.username":"New Username"}})

Please take a look at my last example where I try to figure out how to update this special element "username" of the user with email="[email protected]":

collection.update_one(document{} << "document" << "test" << finalize, 
                      document{} << "$set" << open_document << 
                      (HOW TO DEAL?!) close_document << finalize);

1 Comment

This is not an answer. Use the edit button on the question and add it to the question itself. Then delete this answer.

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.