M220P User Report

I am struggling with setting the read concern…

Earlier in the class to set write concern you use this:

db.users.with_options(write_concern = WriteConcern(w = "majority")).insert_one({
            "name": name,
            "email": email,
            "password": hashedpw
        })

However, in this read example… It appears I am supposed to set the read concern in the variable rc… The all the documentation I have read show the setting of the read concern to be as follows.

db.collection.with_options(read_concern.ReadConcern(level : majority)

However I cannot get this to work?

What am I doing wrong.

Also… In the documentation, it mentions primary preferred, and secondary preferred along with primary and secondary… But the documentation on read preferences shows majority… Am I looking at the wrong documentation?

Hey @David_Thompson,

The lecture and lab mentions doing something like this:

rc = ReadConcern("majority")
comments = db.comments.with_options(read_concern=rc)

You are almost there! :dizzy: It should be:

db.<collection_name>.with_options(read_concern = ReadConcern("majority")

Seems like you are confusing Read Preference documentation with Read Concern. This lab is focused on Read Concern. I am attaching a few links for you to help you cement your knowledge of both. :smile:

  1. Read Concern
  2. Pymongo Read Concern Documentation
  3. Read Preference
  4. Pymongo Read Preference

Feel free to reach out for anything else as well.

Regards,
Satyam

Thank you … I do get them mixed up…

My question now is why would I set the read concern when I could just use read preference? It seems that for reports and such where there isn’t a need for speed (admins will run it) that I could accomplish the same with changing the read preference… That way I will read off the secondary and by that ensure there is read isolation (I am getting the best data).

Or is this not a good practice.

Hey @David_Thompson,

It seems like there’s still some confusion between the two.

Read preference specifies how the read operation is directed to the primary or secondary or to the other nodes.
The primary read preference is the default mode and concerns MongoDB clients; it’s a driver/client option . That means you read data from the master instance where it is written first (before replicated to other replica set members).
If you use other modes than the primary read preference then you risk reading stale data.

Read concern is a query option for replica sets. By default, the read concern is local . This returns the most recent data available at the time of the query execution. The data may not have been persisted to the majority of the replica set members and may be rolled back. The option can be set to majority , which will make the query read the most recent data that has been persisted to the majority of the replica set members and will not be rolled back.

You can read from the documentations attached in my previous reply too. Let me know if there’s any more doubts around this.

Regards,
Satyam

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