How to correctly use db.collection.find().snapshot() in the query

67 views
Skip to first unread message

olis...@gmail.com

unread,
Jan 12, 2018, 8:30:02 AM1/12/18
to mongodb-user



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.

Stephen Steneker

unread,
Jan 12, 2018, 3:48:03 PM1/12/18
to mongodb-user
On Saturday, 13 January 2018 00:30:02 UTC+11, olisehrac wrote:
I want to use db.collection.find().snapshot() to keep the old image in the query. 

Hi,

The snapshot cursor option does not provide query isolation or transactional semantics: it only ensures that a query does not return the same document multiple times. Storage engines implement this as either a collection scan (WiredTiger) or a traversal of the _id index (MMAPv1).

This option is deprecated as of the MongoDB 3.6 release, with the recommendation to instead use an index hint to achieve an equivalent outcome.

See DOCS-11201 for more information.

Regards,
Stennie

olis...@gmail.com

unread,
Jan 20, 2018, 7:12:13 AM1/20/18
to mongodb-user
Hi,Stephen
Thank you for your reply.

在 2018年1月13日星期六 UTC+8上午4:48:03,Stephen Steneker写道:
Reply all
Reply to author
Forward
0 new messages