Mongodb map _id to _id

Hello @EvanWellens-2911 ,

Thank you for posting query in Microsoft Q&A Platform.

Data Factory automatically generates an _id for a document if an _id isn't specified either in the original document or by column mapping.

So, If you don't want ADF to generate _id then you should consider column mapping or having _id column in source.

Please Note, to achieve schema-agnostic copy, skip the "structure" (also called schema) section in dataset and schema mapping in copy activity.

Click here to know more about Mongo DB connector in Azure data factory.
Click here to know about Mongo DB as Sink dataset properties in Azure data factory.

Hope this helps. Please let us know if any further queries. Thank you.


  • Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.

  • Want a reminder to come back and check responses? Here is how to subscribe to a notification.

Our stance on the current iteration of the schema language is pretty firm in reducing magic opinionations. The DSL should be consistent for a broad variety of connectors with no need to learn intricacies and caveats when using different connectors. There is 100% a discussion to be had about manageability of the schema when it gets unwieldy like id String @id @default(dbgenerated()) @map("_id") @db.ObjectId.

why isn't it required to specify INT as a type for the SQL databases? like in PG you can set ID type to string. Prisma just assumes that a default type is an integer. why then it can't assume that the default type for Mongo id is ObjectID? just use it as a default, but allow people to override it if they wanna use any other type

This would be inconsistent with the DSL type representation and I hope the explanation above sheds some light on this. A string is a string and nothing specialized like an ObjectID, this default is consistent in the Prisma world.

If you are really unhappy about the current ergonomics of the Mongo ID, then I propose you just alias it and be done with it (yes, this exists and works already):

type MongoID = String @id @default(dbgenerated()) @map("_id") @db.ObjectId

model User {
  id   MongoID
  name String
}

The above is an undocumented schema feature and we will make breaking changes to this in the future.



We have been using MongoDB Object Id in all the previous chapters. In this chapter, we will understand the structure of ObjectId.

An ObjectId is a 12-byte BSON type having the following structure −

  • The first 4 bytes representing the seconds since the unix epoch
  • The next 3 bytes are the machine identifier
  • The next 2 bytes consists of process id
  • The last 3 bytes are a random counter value

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The complex combination of ObjectId makes all the _id fields unique.

Creating New ObjectId

To generate a new ObjectId use the following code −

>newObjectId = ObjectId()

The above statement returned the following uniquely generated id −

ObjectId("5349b4ddd2781d08c09890f3")

Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id −

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")

Creating Timestamp of a Document

Since the _id ObjectId by default stores the 4-byte timestamp, in most cases you do not need to store the creation time of any document. You can fetch the creation time of a document using getTimestamp method −

>ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()

This will return the creation time of this document in ISO date format −

ISODate("2014-04-12T21:49:17Z")

Converting ObjectId to String

In some cases, you may need the value of ObjectId in a string format. To convert the ObjectId in string, use the following code −

>newObjectId.str

The above code will return the string format of the Guid −

5349b4ddd2781d08c09890f3

CJ

unread,

Aug 31, 2012, 9:18:49 PM8/31/12

to

Hi,

I'm wondering if there's a way to configure mongodb to use "id" instead of "_id" on all the entities or bson documents being stored in the dabtabase?

craiggwilson

unread,

Aug 31, 2012, 11:33:51 PM8/31/12

to

No, there isn't.  What is your reasoning for wanting to do this?

Sam Millman

unread,

Sep 1, 2012, 6:33:03 AM9/1/12

to

I agree it is easy to confuse id with _id in your programming especially if you come form SQL (I do it all the time) also id is sometimes cleaner in code however _id is so enbedded now I don't think there is any serious way to change it.

Though I guess it could be a feature to one day be able to define the name of the default primary key in a collection through config.


CJ

unread,

Sep 1, 2012, 7:52:48 AM9/1/12

to

Sammaye that's a good idea, a configuration file where to define your ways :)

Scott Hernandez

unread,

Sep 1, 2012, 9:24:34 AM9/1/12

to

Generally this is done at the client when you map from bson document
to native object/class.

Russell Bateman

unread,

Sep 1, 2012, 11:21:41 AM9/1/12

to

I use oid, which is merely and painlessly translated to and from _id by my DAO layer (in Java). One need not require the mountain to be moved when there's already a convenient pass right through it.

craiggwilson

unread,

Sep 1, 2012, 1:06:31 PM9/1/12

to

You guys missed the rest of the discussion between CJ and I because he replied via email.  I'll post it here...

So, first off, you can use Id instead of _id.  We (C# driver) will map that by
default.  In addition, you can use [BsonElement("username")] and name
your property with proper C# conventions.  There is also a way to
configure this fluently or with conventions.  I'd suggest you read the
serialization tutorial here:
http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial

For your JsonWriter question, there is a Strict mode you can use with the JsonWriter in
order to get the MongoDBisms out of the way so things will go straight
into json properly.  However, strict mode will not come back to
mongodb with some tweaking.

On Fri, Aug 31, 2012 at 10:52 PM, CJ <> wrote:
> Just wanted to have a clean document and was wondering if that was
> possible...
>
> Another issue is i have the following scenario:
>
> public class User{
>     public Guid _id { get; set; }
>     public string username { get; set; }
>     public string password { get; set; }
> public User(){
> _id = Guid.NewGuid();
> }
> }
>
> var user = new User();
>
> // With settings
> JsonWriterSettings settings = new JsonWriterSettings{OutputMode =
> JsonOutputMode.JavaScript };
> user.ToJson(settings);
> /*
> {
>    "_id":{
>       "$binary":"n2FLBkAkhEOCkX42BGXRqg==",
>       "$type":"03"
>    },
>    "username":"",
>    "password":""
> }
> */
> Is there a way that the _id is simply returned
> "_id":"3F2504E0-4F89-11D3-9A0C-0305E82C3301" so that i can use it nicely in
> javascript and the stored document has its _id field in its binary shorter
> counter part ? instead of plain string guid.