[SERVER-29063] document not inserted properly - a populated ObjectId field is inserted as is, instead of as ObjectId Created: 04/May/17  Updated: 27/Oct/23  Resolved: 04/May/17

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: yi ba Assignee: Mark Agarunov
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

reconstruct:

bulk.insert({ field1: 'abc', field2: { _id: 'someid', value: 'someval' } });

Where field2 is of type ObjectId, and references another table, where 'someid' is the OID of some entry with a single key 'value' with value 'someval' . The collection model corresponds to that.

Expected result:
A new entry in the collection, with field2 as an OID .

Actual result:
A new entry was created in the collection, but the field2 was created as an object with 2 fields, '_id' and 'value' .



 Comments   
Comment by Mark Agarunov [ 04/May/17 ]

Hello yinonby,

Thank you for the report. Looking at the query you've provided, it appears that the behavior you're describing is expected. The way the query is structured,

 { _id: 'someid', value: 'someval' } 

is the value of field2. The _id field of the document would be automatically generated by MongoDB as it is not being set in the query. To explicitly set the _id for the document, it would need to be included in the insert, for example:

bulk.insert({ _id: 'actual_id', field1: 'abc', field2: { _id: 'someid', value: 'someval' } });

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group.

Thanks,
Mark

Comment by yi ba [ 04/May/17 ]

When I use an update with the same type of data, it is converted to an ObjectId.

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