Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:17:17+00:00 2026-06-18T09:17:17+00:00

I have a small problem with ObjectId representation. Here is sample code: public class

  • 0

I have a small problem with ObjectId representation.
Here is sample code:

public class EntityWithObjectIdRepresentation
{
    public string Id { get; set; }

    public string Name { get; set; }
}

[Test]
public void ObjectIdRepresentationTest()
{
    BsonClassMap.RegisterClassMap<EntityWithObjectIdRepresentation>(cm =>
    {
        cm.AutoMap();
        cm.GetMemberMap(x => x.Id).SetRepresentation(BsonType.ObjectId);
    });

    var col = db.GetCollection("test");
    var entity = new EntityWithObjectIdRepresentation();
    col.Insert(entity);

    Assert.IsNotNullOrEmpty(entity.Id); // Ok, Id is generated automatically

    var res = col.FindOneByIdAs<EntityWithObjectIdRepresentation>(entity.Id);
    Assert.IsNotNull(res); // Fails here
}

The code above works fine with

var res = col.FindOneByIdAs<EntityWithObjectIdRepresentation>(ObjectId.Parse(entity.Id));

But what I want is to abstract this stuff in generic repository class, so generally I don’t know whether this Id must be converted to ObjectId or not.
Can I retrieve such information from BsonClassMap?

Following code works too, but due to LINQ expression convertion, it almost in 15 times slower according to benchmarks:

var res = col.AsQueryable().FirstOrDefault(x => x.Id.Equals(id));

OK, I’m including actual code from project:

 public class MongoDbRepository<T, T2> : IRepository<T, T2>
    where T : IEntity<T2> // T - Type of entity, T2 - Type of Id field
{        
    protected readonly MongoCollection<T> Collection;

    public MongoDbRepository(MongoDatabase db, string collectionName = null)
    {
        MongoDbRepositoryConfigurator.EnsureConfigured(db);   // Calls BsonClassMap.RegisterClassMap, creates indexes if needed  

        if (string.IsNullOrEmpty(collectionName))
        {
            collectionName = typeof(T).Name;
        }

        Collection = db.GetCollection<T>(collectionName);
    }

    public T GetById(T2 id)
    {
        using (Profiler.StepFormat("MongoDB: {0}.GetById", Collection.Name))
        {
            // TODO Use FindOneByIdAs<T>
            return Collection.AsQueryable().FirstOrDefault(x => x.Id.Equals(id));
        }
    }

    // some more methods here ...
}

// ...
var repo = new MongoDbRepository<SomeEntity,string>(); // Actually it's injected via DI container
string id = "510a9fe8c87067106c1979de";

// ...
var entity = repo.GetById(id);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:17:18+00:00Added an answer on June 18, 2026 at 9:17 am

    Given a Map:

    var classmap = BsonClassMap.LookupClassMap(typeof(T));
    // // This is an indexed array of all members, so, you'd need to find the Id
    var member = map.AllMemberMaps[0]; 
    var serOpts = ((RepresentationSerializationOptions).SerializationOptions);
    if (serOpts.Representation == BsonType.ObjectId) { ... }
    

    Using the basic logic above, you can determine the serialized type of a member.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have small problem. Code behind: ... public struct Project { string Name; string
I have small problem with my SQL code: UPDATE articles SET like=like+1 WHERE id=1
I have a small problem with interfaces. Here it is in Pseudo code :
I have small problem with displaying cyrillic app name on PlayBook and BB10 simulator.
I have a small problem with VS2012, I've never used it before so... Here
I have a small problem with this code and i cannot understand why is
i have small problem understanding why my smart pointer class is leaking on self
Hello I have small problem with followed C code: //Hardcoded commands: #define someCommand 0x1223;
I'm new to java script i have small problem this little code it's not
I have a small problem. I have this piece of code in Python (taken

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.