Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MongoDB Int Primary Key

MongoDB Int Primary Key

Scheduled Pinned Locked Moved Database
mongodbhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm not too familiar with MongoDB. In our MongoDb we're currently using the default Guid keys. However, I don't see any reason not to use Init PKs. Our base Entity has the following

    public class _EntityBase : _BindableBase
    {
    [BsonId(IdGenerator = typeof(GuidGenerator))]
    [BsonRepresentation(BsonType.String)]
    [DataMember]
    public Guid Id { get; set; }
    }

    Any reason not to change it to ints?

    public class _EntityBase : _BindableBase
    {
    [BsonId(IdGenerator = typeof(MongoDBIntIdGenerator))]
    [BsonRepresentation(BsonType.Int64)]
    public int Id { get; set; }
    }

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    J 1 Reply Last reply
    0
    • K Kevin Marois

      I'm not too familiar with MongoDB. In our MongoDb we're currently using the default Guid keys. However, I don't see any reason not to use Init PKs. Our base Entity has the following

      public class _EntityBase : _BindableBase
      {
      [BsonId(IdGenerator = typeof(GuidGenerator))]
      [BsonRepresentation(BsonType.String)]
      [DataMember]
      public Guid Id { get; set; }
      }

      Any reason not to change it to ints?

      public class _EntityBase : _BindableBase
      {
      [BsonId(IdGenerator = typeof(MongoDBIntIdGenerator))]
      [BsonRepresentation(BsonType.Int64)]
      public int Id { get; set; }
      }

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      Because the key is generated on the client not the server. So if you only have one client, ever, then no not much of a problem. But if you want to add another server then it starts to be a problem. MongoDB integer primary key (nodejs example) using server side functions[^] In general the sort of things that a int id in a SQL database which might be solved are not the same as for NoSQL (mongodb). For example the overhead for storing each document (instance) is already so high that and saving with an int is almost insignificant. And if it is a matter of retrieval speed then look to some other solution that would provide real performance rather than the small (if any) gain that this might produce.

      K 1 Reply Last reply
      0
      • J jschell

        Because the key is generated on the client not the server. So if you only have one client, ever, then no not much of a problem. But if you want to add another server then it starts to be a problem. MongoDB integer primary key (nodejs example) using server side functions[^] In general the sort of things that a int id in a SQL database which might be solved are not the same as for NoSQL (mongodb). For example the overhead for storing each document (instance) is already so high that and saving with an int is almost insignificant. And if it is a matter of retrieval speed then look to some other solution that would provide real performance rather than the small (if any) gain that this might produce.

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        jschell wrote:

        Because the key is generated on the client not the server.

        So what if I created a PK table on the sever:

        public int GetNextPrimaryKey(string collectionName)
        {
        lock(_lockObj)
        {
        // Get the PrimaryKeys collection from the DB
        IMongoCollection pkCol = GetCollection("PrimaryKeys");

            // Attempt to find the row for the collection 
            PrimaryKeyEntity entity = pkCol.Find(x => x.CollectionName == collectionName).FirstOrDefault();
        
            // If it doesn't exist...
            if (entity == null)
            {
                // Create and insert the row for the desired collection
                entity = new PrimaryKeyEntity
                {
                    PrimaryKey = 1,
                    CollectionName = collectionName
                };
                pkCol.InsertOne(entity);
            }
            else
            {
                // Increment the PK
                entity.PrimaryKey = entity.PrimaryKey + 1;
        
                var filter = Builders.Filter.Eq(x => x.CollectionName, collectionName);
                var update = Builders.Update.Set(x => x.PrimaryKey, entity.PrimaryKey);
                var result = pkCol.UpdateOneAsync(filter, update).Result;
            }
        
            return entity.PrimaryKey;
        }
        

        }

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        J 1 Reply Last reply
        0
        • K Kevin Marois

          jschell wrote:

          Because the key is generated on the client not the server.

          So what if I created a PK table on the sever:

          public int GetNextPrimaryKey(string collectionName)
          {
          lock(_lockObj)
          {
          // Get the PrimaryKeys collection from the DB
          IMongoCollection pkCol = GetCollection("PrimaryKeys");

              // Attempt to find the row for the collection 
              PrimaryKeyEntity entity = pkCol.Find(x => x.CollectionName == collectionName).FirstOrDefault();
          
              // If it doesn't exist...
              if (entity == null)
              {
                  // Create and insert the row for the desired collection
                  entity = new PrimaryKeyEntity
                  {
                      PrimaryKey = 1,
                      CollectionName = collectionName
                  };
                  pkCol.InsertOne(entity);
              }
              else
              {
                  // Increment the PK
                  entity.PrimaryKey = entity.PrimaryKey + 1;
          
                  var filter = Builders.Filter.Eq(x => x.CollectionName, collectionName);
                  var update = Builders.Update.Set(x => x.PrimaryKey, entity.PrimaryKey);
                  var result = pkCol.UpdateOneAsync(filter, update).Result;
              }
          
              return entity.PrimaryKey;
          }
          

          }

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Kevin Marois wrote:

          So what if I created a PK table on the sever:

          In this scenario... (some caller) -> (app server) -> (database server) In the above the 'app server' is the 'client' for the 'database server' If you create the key on the 'app server' then you have the following choices (locking in your code has no impact on this.) 1. There can only be one server. 2. You will need to create/find a mechanism the correlates the id creation between multiple 'app server' instances.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups