top of page

MongoDB ChangeStreams - Full Document Lookup

  • Writer: Rishaab
    Rishaab
  • Jun 13, 2021
  • 2 min read

Updated: Jul 16


The change streams by default emits only the delta fields for any update operation, ie. the event with operationType update does not contain the fullDocument field by default.


Consider the below update event. Note the updateDescription field contains only the updated fields and there is no fullDocument field in this event.

{
	"_id" : {
		"_data" : "8260BD54C8000000...",
		"_typeBits" : BinData(0,"QA==")
	},
	"operationType" : "update",
	"clusterTime" : Timestamp(1623020744, 1),
	"ns" : {
		"db" : "test",
		"coll" : "test"
	},
	"documentKey" : {
		"_id" : 0
	},
	"updateDescription" : {
		"updatedFields" : {
			"sample" : "test 2"
		},
		"removedFields" : [ ]
	}
}

What if, you also want to receive all fields in the document after the update? The change streams provides fullDocument option as part of the watch command to accomplish this.


The syntax looks like so,

db.test.watch([<optional user-pipeline>], {fullDocument: <"updateLookup" | "default">})

The fullDocument option can be either "updateLookup" or "default". You should pass "updateLookup" to the fullDocument if you want to receive the full document for every update event. The change streams assumes "default" if you don't specify fullDocument option.


Let's see this in action with our old test collection.


Keep a watch on the collection and tell the change streams that you want to receive the full document for every update event.

let cursor = db.test.watch([], {fullDocument: "updateLookup"})

Add a document and then update its field

db.test.insert({_id: 0, sample: "test"})
db.test.update({_id: 0}, {$set: {sample: "updatedTest"}})

Do cursor.next() until we get the update event. And you should now see the fullDocument field in the update event. This field contains the post image of the document after the update operation.

{
	"_id" : {
		"_data" : "8260C66AD5000000012B...",
		"_typeBits" : BinData(0,"QA==")
	},
	"operationType" : "update",
	"clusterTime" : Timestamp(1623616213, 1),
	"fullDocument" : {
		"_id" : 0,
		"sample" : "updatedTest"
	},
	"ns" : {
		"db" : "test",
		"coll" : "test"
	},
	"documentKey" : {
		"_id" : 0
	},
	"updateDescription" : {
		"updatedFields" : {
			"sample" : "updatedTest"
		},
		"removedFields" : [ ]
	}
}



Recent Posts

See All
BSON - Diving Deep

BSON stands for Binary JSON which is a serialization format for binary-encoding JSON-like documents. BSON was developed at MongoDB in...

 
 
 
MongoDB ChangeStreams - Resuming

Today we are going to explore a very powerful tool in the change streams called the resume token . For motivation purposes, consider,...

 
 
 

Thanks for submitting!

©2023 by Rishab Joshi.

bottom of page