[SERVER-22489] Copy-paste A to B in shell; A != B Created: 05/Feb/16  Updated: 05/Feb/16  Resolved: 05/Feb/16

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

Type: Bug Priority: Major - P3
Reporter: Nathaniel Kofalt Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates MONGOSH-1032 NumberInt object wrapper is not displ... Closed
Operating System: ALL
Steps To Reproduce:

A short demonstration:

// insert a document into a new table with an integer
db.demo.insert({"bar": NumberInt(1)})
//--> WriteResult({ "nInserted" : 1 })
 
// good so far
db.demo.find()
//--> { "_id" : ObjectId("56b4c8127c635f488e84d88c"), "bar" : 1 }
 
// confirm the field is of type int
// ref: https://docs.mongodb.org/manual/reference/operator/query/type
db.demo.count({"bar": {"$type": 16}})
//--> 1
 
// copy-paste the doc from the second query into a second table
db.demo2.insert({ "_id" : ObjectId("56b4c8127c635f488e84d88c"), "bar" : 1 })
//--> WriteResult({ "nInserted" : 1 })
 
// The data has changed!
db.demo2.count({"bar": {"$type": 16}})
//--> 0
 
// This no-longer-equivalent doc instead has a double :(
db.demo2.count({"bar": {"$type": 1}})
//--> 1

Participants:

 Description   

When using the mongo shell, copying a query result into an insert will not yield the same document. In particular, types can change: integer fields will be casted to doubles. To put it simply, if you copy-paste A to B, A ! = B. Yikes!

This was a big surprise to me, and contrasts with other types like object ids and timestamps, which are printed with their wrapping functions. Offhand, I see two options for fixing this:

Option 1: If the shell has a default type interpretation (doubles), non-doubles should be printed in query results wrapped in their function (`NumberInt`). This is consistent with the other aforementioned types, and would make copy-paste work correctly.

Option 2: Queries in the shell could interpret integers as integers, rather than casting everything to a double. I like this option less, as it's a breaking change to shell behavior and could lead to similarly unintuitive behavior for other use cases.



 Comments   
Comment by Kelsey Schubert [ 05/Feb/16 ]

Hi NathanielKofalt,

Thank you for the detailed report: I am closing this a duplicate of SERVER-5234. Please watch SERVER-5234 for updates.

Kind regards,
Thomas

Generated at Thu Feb 08 04:00:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.