Skip to content

Add a student records console application #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 18, 2023

Conversation

bisht2050
Copy link
Contributor

@bisht2050 bisht2050 commented Dec 27, 2022

Will add a permalink to the article (in the file added with this PR) which references this code, once the article is published.


using namespace std;
// Add URI inside the curly braces in below line.
static const mongocxx::uri s_Cluster0_uri = mongocxx::uri{ /*Add Uri here ---> */""};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you replace this with fetching the value from env variable please ? see https://github.com/mongodb-developer/get-started-cxx/blob/main/cxx/getstarted.cpp#L19
Also, I'd suggest to change the variable name to something related to MongoDB URI with a camel case convention (to match the file)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand it would be a good practice to fetch the URI from an external source generally, but could you help me understand the rationale behind fetching it from env variable rather than just mentioning it here? Bearing in mind, this is a program meant for a new dev to get started asap. Adding a env variable into the mix adds one more step for them. There could be more barriers (for example, students working in labs where they may not have access to create new env variables).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this is the simplest way (to type the URI in the code). However there are a number of instances in the past where new developers just copy the example code and ended up committing their Atlas URI on GitHub public repository. Some even think that this method is normal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that's a fair enough reason. I'll update this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
PS: getenv causes compilation error since MSVC flags it as an unsafe method. Using a safer alternative - _dupenv_s

const string collName = "StudentCollection";

auto dbs = getDatabases(conn);
// Check if database already exists.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a comment, generally in MongoDB databases and collections are created automatically if they don't exist.

{
if (doc[key].get_string().value == value)
{
collection.update_one(doc, bsoncxx::builder::stream::document{} << "$set" << bsoncxx::builder::stream::open_document << newKey << newValue << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to separate the find and the update operation ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - Different method for different responsibilities.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is to update one document, the find() operation is redundant, as update_one() could find the document and update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I misunderstood the earlier comment. I'll cross check this in my code and remove find operation if it's really not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed find.


for (auto&& doc : cursor)
{
if (doc[key].get_string().value == value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why there would be a client-side filter here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter avoids potentially scanning through all documents on the client. It was suggested by C++ engineering lead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 106: Find document(s) matching key/value. For example rollNo: foo
Line 108: For each of documents returned by the cursor assign to doc. Say there are 2 documents matching rollNo: foo
Line 110: If the string value from doc object with key rollNo is equal to foo print the document

This condition is redundant because all of the documents returned by the cursor should have key=rollNo with the value of foo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed redundant check.

// ********************************************** Main **********************************************
int main()
{
cout << "Hello World!" << endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be removed ?

}

// Create an instance.
mongocxx::instance inst{};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation for this code block

@sindbach sindbach merged commit 3e5c451 into mongodb-developer:main Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants