Watch keynotes and sessions from, our virtual developer conference.

How to insert BSON record using Go Driver

Is there any way where we can insert a BSON Mongo record using go-mongo -> InsertOne method?

I have a record like this:

		"_id" : ObjectId("5ef59232fe31f24ac6b54822"),
		"batchId" : NumberLong(184486),
		"batchName" : "sample",
		"createdBy" : "createdSample",
		"status" : "CREATED",
		"type" : "normal",
		"batchType" : "Regular",
		"batchStartDate" : ISODate("2020-06-26T00:00:00Z"),
		"batchEndDate" : ISODate("2020-08-20T00:00:00Z"),
		"size" : 10,
		"sector" : {
			"id" : "18",
			"name" : "Food"
		"jobRoles" : [
				"jobName" : "Processed Food",
				"qpCode" : "FIC",
				"version" : "1.0",
				"nsqfLevel" : "6",
				"jobRoleDesc" : "",
				"attendanceUploaded" : null,
				"traingingAttendanceSubmitted" : null,
				"assessmentStartDate" : ISODate("2020-08-23T00:00:00Z"),
				"assessmentEndDate" : ISODate("2020-08-23T00:00:00Z"),
				"isPlatformQP" : false,
				"isBaseQP" : false,
				"isBatchAssigned" : {
					"masterTrainer" : false,
					"assessmentAgency" : false
				"isRejected" : {
					"masterTrainer" : false,
					"assessmentAgency" : false
				"sector" : {
					"id" : "12",
					"name" : "Food Processing"
				"trainingHoursPerDay" : 6,
				"jobRoleCategory" : "3",
				"initialAssessmentStartDate" : ISODate("2020-08-23T00:00:00Z"),
				"initialAssessmentEndDate" : ISODate("2020-08-23T00:00:00Z"),
				"overrideQpHours" : NumberLong(280)

I want to insert this document into MongoDB without the use of struct, wherein the above document will be assigned to a go variable and the record will be inserted using that variable.

Is there any way to achieve this?

Also, I have seen that the bsonx package has a bsonx.JavaScript. Is there any way to inset a bsonx datatype into MongoDB using go

Hi @Harshavardhan_Kumare,

What format is this record in? The example record you gave looks like something printed out by calling collection.find() in the mongo shell, but it’s important for us to know what the exact format is.

For your second question, bsonx is a set of experimental APIs that we don’t recommend for regular use because there’s no stability guarantees. You can use the public primitive.JavaScript type ( to insert BSON JavaScript values. For example:

doc := bson.D{
    {"jsKey", primitive.JavaScript("function() { sleep(1000); }")},

– Divjot

1 Like

Hi @Divjot_Arora,

You are right. The example record I gave is the output from a Mongo Shell findOne command. I want to test the CRUD operations I have written in GoLang. I want to insert many example records for each CRUD operations and since there are so many sample records that I want to insert for testing, it is very difficult for me to convert those example documents to bson.M.

Is there any workaround in achieving this?

Or, is there a better way of testing the update and delete operations?

Thanks a ton for your response.

If these records already exist in your database, you can use Collection.Find during your test setup to fetch these documents from your collection and store them in a slice. Your tests can then use one or more of these documents to call Update/Delete/etc. I’d also recommend looking into the Cursor.All method to quickly convert all documents into a slice of something like bson.D.


Thanks for the idea @Divjot_Arora, this looks like a viable option.