[SERVER-21088] Change _massageObject string processing Created: 23/Oct/15  Updated: 13/Aug/16  Resolved: 09/Aug/16

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 3.3.11

Type: Task Priority: Major - P3
Reporter: Kevin Pulo Assignee: Matt Cotter
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Build B (10/30/15), Build C (11/20/15), Platforms 2016-08-26
Participants:

 Description   

_massageObject (used by db.foo.find() and db.foo.remove()) converts a 24 char string into { _id: "..." }. Surely the intention behind the length == 24 check is to convert the string into the corresponding ObjectId()?

src/mongo/shell/collection.js:134-158

 DBCollection.prototype._massageObject = function( q ){
     if ( ! q )
         return {};
 
     var type = typeof q;
 
     if ( type == "function" )
         return { $where : q };
 
     if ( q.isObjectId )
         return { _id : q };
 
     if ( type == "object" )
         return q;
 
     if ( type == "string" ){
         if ( q.length == 24 )
-            return { _id : q };
+            return { _id : ObjectId( q ) };
 
         return { $where : q };
     }
 
     throw Error( "don't know how to massage : " + type );
 
 }



 Comments   
Comment by Githook User [ 09/Aug/16 ]

Author:

{u'username': u'Machyne', u'name': u'Matt Cotter', u'email': u'matt.cotter@mongodb.com'}

Message: SERVER-21088 convert to objectid in massage object
Branch: master
https://github.com/mongodb/mongo/commit/df436df641a34cd8b84ba6dbdd04eac63e1ba8cf

Comment by Matt Cotter [ 09/Aug/16 ]

Surely the intention behind the length == 24 check is to convert the string into the corresponding ObjectId()?

I totally agree here. Also it's kind of a nice feature to have:

> db.example.insert({ hi: "findMePlzzzz" })
WriteResult({ "nInserted" : 1 })
> db.example.find()
{ "_id" : ObjectId("57a9e92d2e307112e6a11fc0"), "hi" : "findMePlzzzz" }
> // do other stuff, but copy paste the above id
> db.example.find('57a9e92d2e307112e6a11fc0')  // cuz why not?
{ "_id" : ObjectId("57a9e92d2e307112e6a11fc0"), "hi" : "findMePlzzzz" }

I don't see a good reason not to do this

Comment by Scott Hernandez (Inactive) [ 23/Oct/15 ]

Maybe, but since it doesn't work that way now, it is probably better to just remove the string check all together.

Generated at Thu Feb 08 03:56:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.