about the uint32 and long and string data type in c#

511 views
Skip to first unread message

zhengchun

unread,
Mar 19, 2012, 8:33:31 AM3/19/12
to mongod...@googlegroups.com
i knowed that mongodb not support uint32 type,just only support int32,long and double.

recently,i implement the mongodb in my project,i get a problem,the unique id with uint32 type that was generated by the hash function(FNV) and as "_id" field value.now,if just as uint32 store,the mongodb will automatic convert to the int type and cut out.so,i have two solution:

1.convert uint32 to long type.
2.convert uint32 to string.(current i use this method,but i don't think this is best)

so which method is the best practical,i always query on this _id ,in the furture,the _id will as the sharding key.

thanks for your advice.


Robert Stam

unread,
Mar 19, 2012, 9:18:05 AM3/19/12
to mongod...@googlegroups.com
It is possible to store uint32 values using a 32 bit BSON int, but it involves some tricks.

If you are using a C# class you have to tell the driver that you want to allow overflow. For example:

    public class C
    {
        [BsonRepresentation(BsonType.Int32, AllowOverflow = true)]
        public uint Id;
        public string Data;
    }

    uint id = 2222222222; // too large for int32 but OK for uint32
    var c = new C { Id = id, Data = "abc" };
    collection.Insert(c);

If you are using lower level BsonDocuments you have to cast your uint id value to an int yourself:

    uint id = 2222222223; // too large for int32 but OK for uint32
    var document = new BsonDocument
    {
        { "_id", (int) id },
        { "Data", "def" }
    };

You also have to keep in mind that when you display large uint32 values in the mongo shell (values that have the high order bit set) they are going to display as negative numbers:

> db.test.find()
{ "_id" : -2072745074, "Data" : "abc" }
{ "_id" : -2072745073, "Data" : "def" }
>

When these negative numbers are read back in to your C# program they will map back to the same uint32 they came from, but the fact that they display as negative numbers in the shell is going to make it difficult to work with your data in the shell.

So while it is possible to do this I wouldn't recommend it unless you really need to save space. It would be much easier to just choose a 64 bit integer key instead if a signed 32 bit integer doesn't have a large enough range for you, so I would recommend your option 1 (convert uint32 to long).



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/8TtaqIzmcKMJ.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

zhengchun

unread,
Mar 19, 2012, 10:44:02 AM3/19/12
to mongod...@googlegroups.com
hi,Robert,thanks for your answer.

在 2012年3月19日星期一UTC+8下午8时33分31秒,zhengchun写道:
Reply all
Reply to author
Forward
0 new messages