question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use MongoDB.Bson.ObjectId as Id

See original GitHub issue

How can I use MongoDB.Bson.ObjectId as an Entity Id instead of a string?

I tried this:

public class Query : IEntity { [BsonId, ObjectId] public ObjectId Id { get; set; } }

but this returns null: var query = MongoDB.Entities.DB.Queryable<Query>().FirstOrDefault(x => x.Id == queryID);

This is possible in the offical MongoDB.Driver, please implement it…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dj-nitehawkcommented, Sep 30, 2020

migrating an existing project will be a pain either way 😃 some compromises had to be made with the flexibility to support the features of this library.

1reaction
dj-nitehawkcommented, Sep 30, 2020

here’s an example:

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Entities;
using System.Linq;
using System.Threading.Tasks;

namespace TestProg
{
    internal static class Program
    {
        public class Author : Entity
        {
            public ObjectId BookID { get; set; }
        }

        private static async Task Main()
        {
            await DB.InitAsync("test", "localhost");

            var id = ObjectId.GenerateNewId();

            await new Author { BookID = id }.SaveAsync();

            var res = await DB.Queryable<Author>()
                              .Where(a => a.BookID == id)
                              .FirstOrDefaultAsync();
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ObjectId() — MongoDB Manual
For timestamp and counter values, the most significant bytes appear first in the byte sequence (big-endian). This is unlike other BSON values, where...
Read more >
MongoDB\BSON\ObjectId - Manual
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document...
Read more >
Quick Start: BSON Data Types - ObjectId
ObjectId is one data type that is part of the BSON Specification that MongoDB uses for data storage. It is a binary representation...
Read more >
ObjectId — MongoDB Manual
ObjectId is a 12-byte BSON type, constructed using: ... In MongoDB, documents stored in a collection require a unique _id field that acts...
Read more >
What Is Objectid in Mongodb and How to Generate ...
MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found