Skip to content

DOCSP-41170 - Clarify string ID generation #365

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
Dec 19, 2024

Conversation

mongoKart
Copy link
Collaborator

@mongoKart mongoKart commented Dec 9, 2024

Pull Request Info

PR Reviewing Guidelines

JIRA - https://jira.mongodb.org/browse/DOCSP-41170

Staging Links

  • fundamentals/serialization/poco
  • Self-Review Checklist

    • Is this free of any warnings or errors in the RST?
    • Did you run a spell-check?
    • Did you run a grammar-check?
    • Are all the links working?
    • Are the facets and meta keywords accurate?

    Copy link

    netlify bot commented Dec 9, 2024

    Deploy Preview for mongodb-docs-csharp ready!

    Name Link
    🔨 Latest commit f78e6df
    🔍 Latest deploy log https://app.netlify.com/sites/mongodb-docs-csharp/deploys/67645b17ab12e20008748996
    😎 Deploy Preview https://deploy-preview-365--mongodb-docs-csharp.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    Copy link
    Collaborator

    @jordan-smith721 jordan-smith721 left a comment

    Choose a reason for hiding this comment

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

    LGTM w/ two small things


    .. list-table::
    :header-rows: 1
    :widths: 10 10
    :widths: 25 75

    * - ``Id`` Field Data Type
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    I can't immediately see why, but this is rendering without a space after "Id". Maybe because of the monospace?

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    i couldn't get this to look right so i just removed the monospace. seems like that actually matches the style guide too


    * - ``ObjectId``
    - The driver automatically uses the ``ObjectIdGenerator`` type for ``Id`` fields with
    the ``ObjectId`` data type, like the one in the following code example:
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    https://www.mongodb.com/docs/meta/style-guide/terminology/alphabetical-terms/#std-term-like

    Suggested change
    the ``ObjectId`` data type, like the one in the following code example:
    the ``ObjectId`` data type, such as the one in the following code example:

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    boo this busted rule

    @rishitb-mongodb
    Copy link
    Collaborator

    Could/Should we mention the example mentioned in the ticket as well that would allow users to define the generator in the attribute itself [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]

    @mongoKart
    Copy link
    Collaborator Author

    Could/Should we mention the example mentioned in the ticket as well that would allow users to define the generator in the attribute itself [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]

    For my understanding: What is the use case for this attribute? Do you need to do this in addition to the [BsonRepresentation] attribute in order to serialize to a string? Or can you use this attribute instead of the other one?

    @rishitb-mongodb
    Copy link
    Collaborator

    I'm actually not sure. Maybe @rstam can help. What's the difference between using [BsonRepresentation(BsonType.ObjectId)] on a string ID field as opposed to the attribute [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]? If they are the same then we can ignore adding the alternative way, if they are different then its worth mentioning both and the difference between them.

    Copy link
    Collaborator

    @rstam rstam left a comment

    Choose a reason for hiding this comment

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

    LGTM

    @rstam
    Copy link
    Collaborator

    rstam commented Dec 11, 2024

    What's the difference between using [BsonRepresentation(BsonType.ObjectId)] on a string ID field as opposed to the attribute [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]?

    They are not the same. Using BsonId with StringObjectIdGenerator without configuring the representation would store the ObjectIds in the database as strings.

    BsonRepresentation(BsonType.ObjectId) stores the strings as ObjectIds in the database.

    They are used together. Though unless you have changed the default conventions there is a convention that automatically sets the id generator for a string stored as an ObjectId to the StringObjectIdGenerator .

    @rishitb-mongodb
    Copy link
    Collaborator

    Thanks Robert. @mongoKart I think its worth adding in a callout then in case a user wants to store the ID as a string along with representing it as a string in their POCO.

    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    @mongoKart
    Copy link
    Collaborator Author

    @rishitb-mongodb @rstam Could you take another look?

    @mongoKart mongoKart requested a review from rstam December 13, 2024 17:48
    default value. Instead, the driver generates a unique ID value for the property.

    When you apply the ``[BsonId]`` attribute to a property, you can pass the ``IdGenerator``
    argument to specify the type of ID value the driver generates for the corresponding
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Not "the type of ID value"

    The type is already determined by the type of the property.

    What are you specifying is which generator to use to generate an Id value when needed.

    }

    * - ``string``
    - To serialize the ``Id`` property as a ``string``, apply the
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Applying the [BsonId] attribute is NOT what causes this property to be serialized as a string.

    All strings are serialized as strings UNLESS the [BsonElement] attribute specifies a different representation.

    The ONLY thing this [BsonId] attribute is specifying is which id generator to use (beside identifying this as the ID field of course).

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    After looking at the code for the Generator classes, I see that I've been conflating the ID generation and serialization steps.

    To serialize the ``Id`` property as an ``ObjectId``, apply the
    ``[BsonRepresentation(BsonType.ObjectId)]`` attribute to the property, as shown
    in the following code example. In this case, if the property contains the value
    ``null``, the driver uses the ``ObjectIdGenerator`` type to generate a unique
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Actually it uses the StringObjecIdGenerator.

    The ObjectIdGenerator can only be used with properties of type ObjectId.


    If you apply both the ``[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]``
    and ``[BsonRepresentation(BsonType.ObjectId)]`` attributes to the ``Id`` property,
    the driver generates an ``ObjectId`` value for the field.
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Not sure this is worded the best way.

    If you apply both you have simply done TWO different things:

    1. Configured the representation of the ID in the database
    2. Configured the id generator to use for missing ID values

    @@ -247,7 +248,7 @@ The following code sample maps the ``Identifier`` property to the
    public string Identifier { get; set; }
    }

    .. warning:: Multiple Id Fields
    .. warning:: Multiple ID Fields
    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    Changed most occurrences of 'Id' to 'ID' since you can use any name for an ID property

    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    But... the C#/.Net naming convention is to use Id.

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    After talking to @jordan-smith721, keeping as 'ID' to comply with our Style Guide

    Copy link
    Collaborator

    @rstam rstam left a comment

    Choose a reason for hiding this comment

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

    I have two minor comments but if you want to push as is LGTM.

    @@ -247,7 +248,7 @@ The following code sample maps the ``Identifier`` property to the
    public string Identifier { get; set; }
    }

    .. warning:: Multiple Id Fields
    .. warning:: Multiple ID Fields
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    But... the C#/.Net naming convention is to use Id.

    Every document in a MongoDB collection must have a unique ID. When you serialize an object
    to a collection, if its ID property contains the default
    value for its data type (usually ``null``), the {+driver-short+} doesn't serialize the
    default value. Instead, the driver generates a unique ID value and assigns it to the
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    The C# driver TRIES to generate a unique Id value but can onlly do so in cases where it knows what Id generator to use.

    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    github-actions bot pushed a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit that referenced this pull request Dec 19, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit to mongoKart/docs-csharp that referenced this pull request Dec 20, 2024
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    (cherry picked from commit 30ff13b)
    
    Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com>
    mongoKart added a commit to mongoKart/docs-csharp that referenced this pull request May 16, 2025
    Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants