Dart DocumentationbsonObjectId

ObjectId class

class ObjectId extends BsonObject{
 BsonBinary id;

 ObjectId({bool clientMode: false}){
   int seconds = new Timestamp(null,0).seconds;
   id = createId(seconds, clientMode);
 }

 ObjectId.fromSeconds(int seconds, [bool clientMode = false]){
   id = createId(seconds, clientMode);
 }
 
 ObjectId.fromBsonBinary(this.id);

 BsonBinary createId(int seconds, bool clientMode) {
   getOctet(int value) {
     String res = value.toRadixString(16);
     while (res.length < 8) {
       res = '0$res';
     }
     return res;
   }
   if (clientMode) {
     String s = '${getOctet(seconds)}${getOctet(_Statics.RandomId)}${getOctet(_Statics.nextIncrement)}';
     return new BsonBinary.fromHexString(s);
   } else {
     return new BsonBinary(12)
     ..writeInt(seconds,endianness: Endianness.BIG_ENDIAN)
     ..writeInt(_Statics.RandomId)
     ..writeInt(_Statics.nextIncrement,endianness: Endianness.BIG_ENDIAN);
   }
 }

 factory ObjectId.fromHexString(String hexString) {
   return new ObjectId.fromBsonBinary(new BsonBinary.fromHexString(hexString));
 }
 
 static ObjectId parse(String hexString) => new ObjectId.fromHexString(hexString);

 int get hashCode => id.hexString.hashCode;
 bool operator ==(other) => other is ObjectId && toHexString() == other.toHexString();
 String toString() => 'ObjectId("${id.hexString}")';
 String toHexString() => id.hexString;
 int get typeByte => _BSON_DATA_OID;
 get value => this;
 int byteLength() => 12;
 
 unpackValue(BsonBinary buffer){
    id.byteList.setRange(0,12,buffer.byteList,buffer.offset);
    buffer.offset += 12;
 }
 
 packValue(BsonBinary buffer){
   if (id.byteList == null) {
     id.makeByteList();
   }
   buffer.byteList.setRange(buffer.offset,buffer.offset+12,id.byteList);
   buffer.offset += 12;
 }

 String toJson() => toString();

 // Equivalent to mongo shell's "getTimestamp".
 DateTime get dateTime => new DateTime.fromMillisecondsSinceEpoch(int.parse(id.hexString.substring(0, 8), radix:16) * 1000);

}

Extends

BsonObject > ObjectId

Static Methods

ObjectId parse(String hexString) #

static ObjectId parse(String hexString) => new ObjectId.fromHexString(hexString);

Constructors

new ObjectId({bool clientMode: false}) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
ObjectId({bool clientMode: false}){
 int seconds = new Timestamp(null,0).seconds;
 id = createId(seconds, clientMode);
}

new ObjectId.fromBsonBinary(BsonBinary id) #

ObjectId.fromBsonBinary(this.id);

factory ObjectId.fromHexString(String hexString) #

factory ObjectId.fromHexString(String hexString) {
 return new ObjectId.fromBsonBinary(new BsonBinary.fromHexString(hexString));
}

new ObjectId.fromSeconds(int seconds, [bool clientMode = false]) #

ObjectId.fromSeconds(int seconds, [bool clientMode = false]){
 id = createId(seconds, clientMode);
}

Properties

final DateTime dateTime #

DateTime get dateTime => new DateTime.fromMillisecondsSinceEpoch(int.parse(id.hexString.substring(0, 8), radix:16) * 1000);

final int hashCode #

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int get hashCode => id.hexString.hashCode;

BsonBinary id #

BsonBinary id

final int typeByte #

int get typeByte => _BSON_DATA_OID;

final value #

get value => this;

Operators

bool operator ==(other) #

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw or return null.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so equality of two objects should not change over time, or at least only change if one of the objects was modified.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(other) => other is ObjectId && toHexString() == other.toHexString();

Methods

int byteLength() #

int byteLength() => 12;

BsonBinary createId(int seconds, bool clientMode) #

BsonBinary createId(int seconds, bool clientMode) {
 getOctet(int value) {
   String res = value.toRadixString(16);
   while (res.length < 8) {
     res = '0$res';
   }
   return res;
 }
 if (clientMode) {
   String s = '${getOctet(seconds)}${getOctet(_Statics.RandomId)}${getOctet(_Statics.nextIncrement)}';
   return new BsonBinary.fromHexString(s);
 } else {
   return new BsonBinary(12)
   ..writeInt(seconds,endianness: Endianness.BIG_ENDIAN)
   ..writeInt(_Statics.RandomId)
   ..writeInt(_Statics.nextIncrement,endianness: Endianness.BIG_ENDIAN);
 }
}

dynamic packElement(String name, buffer) #

inherited from BsonObject
packElement(String name, var buffer){
 buffer.writeByte(typeByte);
 if (name != null){
   new BsonCString(name).packValue(buffer);
 }
 packValue(buffer);
}

dynamic packValue(BsonBinary buffer) #

packValue(BsonBinary buffer){
 if (id.byteList == null) {
   id.makeByteList();
 }
 buffer.byteList.setRange(buffer.offset,buffer.offset+12,id.byteList);
 buffer.offset += 12;
}

String toHexString() #

String toHexString() => id.hexString;

String toJson() #

String toJson() => toString();

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => 'ObjectId("${id.hexString}")';

_ElementPair unpackElement(buffer) #

inherited from BsonObject
_ElementPair unpackElement(buffer){
 _ElementPair result = new _ElementPair();
 result.name = buffer.readCString();
 unpackValue(buffer);
 result.value = value;
 return result;
}

dynamic unpackValue(BsonBinary buffer) #

unpackValue(BsonBinary buffer){
  id.byteList.setRange(0,12,buffer.byteList,buffer.offset);
  buffer.offset += 12;
}