-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
DevProd Correctness
-
ALL
-
None
-
None
-
None
-
None
-
None
-
None
-
None
When inserting a BSON object (say the result of a query) through the shell's insert method, the shell can silently change the types of some fields. For example, if I run a query that returns a document containing a NumberInt field (BSON type 16), and then I insert that document into another collection, the field will become a double (BSON type 1). This happens specifically when the object being inserted is lacking an _id field so it goes through addIdIfNeeded in either crud_api.js or bulk_api.js.
The attached test illustrates this. AFAIK the shell doesn't expose ways to inspect BSON, so it's not the clearest illustration. But basically, we:
1. Insert a JS object obj containing a NumberInt field into coll1.
2. Fetch that document from coll1 using aggregate, projecting away it's _id field. This yields a BSON object fetchedObj with a NumberInt field.
3. Insert fetchedObj into coll2 using DBCollection.insert.
4. Fetch that document from coll2 , projecting away it's _id field. This yields a BSON object refetchedObj with a NumberDouble field.
5. Compare fetchedObj and refetchedObj with bsonBinaryEqual.
Intuitively, one would expect them to be bit-for-bit identical in step (5), but they aren't because refetchedObj has a NumberDouble field while fetchedObj has a NumberInt field. This is because fetchedObj went through addIdIfNeeded in step 3. However, if we directly run an insert command in step (3) instead of using the DBCollection.insert method, the two objects will be identical in step (5).