MongoDB Int Primary Key
-
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.
-
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.
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.
-
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.
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.
-
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.
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.