Case1:
MongoDB Enterprise > db.coll.insert({x:1})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.coll.find()
{ "_id" : ObjectId("5a0581f21ef47834e0281945"), "x" : 1 }
MongoDB Enterprise > var j = db.coll.findOne()
MongoDB Enterprise > j
{ "_id" : ObjectId("5a0581f21ef47834e0281945"), "x" : 1 }
MongoDB Enterprise > j.y=2
2
MongoDB Enterprise > j
{ "_id" : ObjectId("5a0581f21ef47834e0281945"), "x" : 1, "y" : 2 }
MongoDB Enterprise > db.coll.save(j)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.coll.find()
{ "_id" : ObjectId("5a0581f21ef47834e0281945"), "x" : 1, "y" : 2 }
Case2:
MongoDB Enterprise > var j = db.coll.find({x:1})
MongoDB Enterprise > j
{ "_id" : ObjectId("5a0581f21ef47834e0281945"), "x" : 1, "y" : 2 }
MongoDB Enterprise > j.z=3
3
MongoDB Enterprise > db.coll.save(j)
2017-11-10T16:11:26.377+0530 E QUERY [thread1] Error: can't save a DBQuery object :
DBCollection.prototype._validateObject@src/mongo/shell/collection.js:231:15
DBCollection.prototype._validateForStorage@src/mongo/shell/collection.js:245:5
Bulk/this.insert@src/mongo/shell/bulk_api.js:652:17
DBCollection.prototype.insert@src/mongo/shell/collection.js:344:13
DBCollection.prototype.save@src/mongo/shell/collection.js:596:16
@(shell):1:1
The only difference between case1 and case2 is the way value is stored in var j. Can someone pls help understand what is wrong in case2 that i am unable to save the document. Thanks in advance.