using MongoDB.Bson; using MongoDB.Bson.IO; // Assuming that `bsonData` is a byte array of a BSON document that contains an ObjectId value var bsonReader = new BsonBinaryReader(bsonData); bsonReader.ReadStartDocument(); var objectId = bsonReader.ReadObjectId("objectIdFieldName"); bsonReader.ReadEndDocument();
using MongoDB.Bson; using MongoDB.Bson.IO; using System.IO; // Assuming that `jsonString` is a JSON string that contains an ObjectId value var bsonDocument = BsonDocument.Parse(jsonString); var bsonReader = new BsonDocumentReader(bsonDocument); bsonReader.ReadStartDocument(); var objectId = bsonReader.ReadObjectId("objectIdFieldName"); bsonReader.ReadEndDocument();In this example, we parse a JSON string into a BsonDocument object, create a new BsonDocumentReader object with the BsonDocument, read the start of the document, read the ObjectId value with the name "objectIdFieldName", and read the end of the document. Both examples use the MongoDB.Bson.dll package/library.