受欢迎的博客标签

MongoDb ObjectID series:MongoDb ObjectID

Published

 

public ObjectId Id {get; set;}

//or

[BsonId]
public string Id {get; set;}

//or

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id {get; set;}

//or

[BsonId]
[BsonRepresentation(BsonType.String)]
public string Id {get; set;}

 

 

字段映射
ID

 the Id property:

Is required for mapping the Common Language Runtime (CLR) object to the MongoDB collection.

 If you have a column named Id, id or _id, in your strongly typed TDocument class (the item type in a collection), then a column named "_id" will be generated in Mongo. It will also create an index for that column. You get a duplicate key error exception if trying to insert an item with a key that already exists.


MongoDB 数据集中存放的数据,称之为文档(Document)。每个文档在存放时,都需要有一个ID,而这个 ID 的名称,固定叫 _id,类型是 MongoDB.Bson.ObjectId。

当建立映射时,如果给出 _id 字段,则 MongoDB 会采用这个 ID 做为这个文档的 ID ,如果不给出,MongoDB 会自动添加一个 _id 字段。在使用上是完全一样的。唯一的区别是,如果映射类中不写 _id,则 MongoDB 自动添加 _id 时,会用 ObjectId 作为这个字段的数据类型。

ObjectId 是一个全局唯一的数据。

MongoDB 允许使用其它类型的数据作为 ID,例如:string,int,long,GUID 等,但这就需要你自己去保证这些数据不超限并且唯一。

BsonId属性

1.the Id property Is annotated with [BsonId] to designate this property as the document's primary key

2.ObjectId 别名:可以在类中修改 _id 名称为别的名称,但需要加一个描述属性 BsonId,BsonId 属性会告诉映射,topic_id 就是这个文档数据的ID 。MongoDB在保存时,会将这个 topic_id 转成 _id 保存到数据集中。

BsonRepresentation属性

[BsonRepresentation] lets you juggle with the Mongo type vs the internal .Net type, if there's a conversion between them.

用来在映射类中的数据类型和数据集中的数据类型做转换。

 

the Id property Is annotated with [BsonRepresentation(BsonType.ObjectId)] to allow passing the parameter as type string instead of an ObjectId structure. Mongo handles the conversion from string to ObjectId.

 

 

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace Nop.Core
{
    public abstract class ParentEntity
    {
        //[BsonId] to designate this property as the document's primary key.
        [BsonId]   

        // [BsonRepresentation(BsonType.ObjectId)] to allow passing the parameter as type string instead of an ObjectId structure. 
       //Mongo handles the conversion from string to ObjectId.
        [BsonRepresentation(BsonType.ObjectId)]  
        public string  _id { get; set; } 

    }


}

Select a specifid "Id" and add it the url, i.e.

http://localhost:5004/api/speaker/54b7d51140c10266ffa3b04d and a single item is returned.

{
    "_id": "54b7d51140c10266ffa3b04d",  <---here is _id
    "First": "Shayne",
    "Last": "Boyer",
    "Twitter": "@spboyer",
    "Title": "Developer Guy",
    "Blog": "tattoocoder.com"
}

在类中修改 _id 名称为别的名称

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace Nop.Core
{
    public abstract class ParentEntity
    {
        [BsonId]   //[BsonId] to designate this property as the document's primary key.
        [BsonRepresentation(BsonType.ObjectId)]  // [BsonRepresentation(BsonType.ObjectId)] to allow passing the parameter as type string instead of an ObjectId structure. Mongo handles the conversion from string to ObjectId.
        public string Id { get; set; }  //可以在类中修改 _id 名称为别的名称,但需要加一个描述属性 BsonId,BsonId 属性会告诉映射,topic_id 就是这个文档数据的ID 。MongoDB在保存时,会将这个 topic_id 转成 _id 保存到数据集中。

    }


}

Select a specifid "Id" and add it the url, i.e.

http://localhost:5004/api/speaker/54b7d51140c10266ffa3b04d and a single item is returned.

{
    "Id": "54b7d51140c10266ffa3b04d",  <---here is Id
    "First": "Shayne",
    "Last": "Boyer",
    "Twitter": "@spboyer",
    "Title": "Developer Guy",
    "Blog": "tattoocoder.com"
}

 

 

 

https://syxdevcode.github.io/2020/09/28/MongoDB%20Dotnet%20Core%E6%95%B0%E6%8D%AE%E6%98%A0%E5%B0%84/

https://www.cnblogs.com/erlongxizhu-03/p/12891493.html