I want to use db.collection.find().snapshot() to keep the old image in the query. Here I opened two sessions in my test environment:
--session 1:
pradb>db.t1225_2.find()
{ "_id" : ObjectId("5a40b358f34facabd6036d27"), "f1" : "a" }
{ "_id" : ObjectId("5a40b358f34facabd6036d28"), "f2" : "b" }
{ "_id" : ObjectId("5a40b378f34facabd6036d29"), "f1" : "d" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2a"), "f2" : "e" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2b"), "f3" : "f" }
pradb>db.t1225_2.find().snapshot() <---perform a snapshot operation
{ "_id" : ObjectId("5a40b358f34facabd6036d27"), "f1" : "a" }
{ "_id" : ObjectId("5a40b358f34facabd6036d28"), "f2" : "b" }
{ "_id" : ObjectId("5a40b378f34facabd6036d29"), "f1" : "d" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2a"), "f2" : "e" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2b"), "f3" : "f" }
--session 2: update field f1 in session 2
pradb>db.t1225_2.update({f1:'a'},{$set:{f1:'A'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
pradb>db.t1225_2.find()
{ "_id" : ObjectId("5a40b358f34facabd6036d27"), "f1" : "A" }
{ "_id" : ObjectId("5a40b358f34facabd6036d28"), "f2" : "b" }
{ "_id" : ObjectId("5a40b378f34facabd6036d29"), "f1" : "d" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2a"), "f2" : "e" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2b"), "f3" : "f" }
--session 1:Back to session 1 invoke the query again but receive the new result
pradb>db.t1225_2.find().snapshot()
{ "_id" : ObjectId("5a40b358f34facabd6036d27"), "f1" : "A" } <---f1:"A" is the new result instead of the before-image when we perform the first snapshot who's value is f1:"a"
{ "_id" : ObjectId("5a40b358f34facabd6036d28"), "f2" : "b" }
{ "_id" : ObjectId("5a40b378f34facabd6036d29"), "f1" : "d" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2a"), "f2" : "e" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2b"), "f3" : "f" }
pradb>db.t1225_2.find()
{ "_id" : ObjectId("5a40b358f34facabd6036d27"), "f1" : "A" }
{ "_id" : ObjectId("5a40b358f34facabd6036d28"), "f2" : "b" }
{ "_id" : ObjectId("5a40b378f34facabd6036d29"), "f1" : "d" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2a"), "f2" : "e" }
{ "_id" : ObjectId("5a40b378f34facabd6036d2b"), "f3" : "f" }
It seems the snapshot() operation does not work as we expected since it doesn't save the before-image.
Any suggestion about this ?
Thanks.