CosmosDB 插入具有特定 ID 的项

huangapple go评论72阅读模式
英文:

CosmosDB insert item with specific ID

问题

在插入新对象到Cosmos DB时是否可以指定ID?

英文:

I was wondering if it is possible to specific the ID when inserting a new object on Cosmos DB.

this.container.items
      .create<Session>(session)
      .then(() => callback(null))
      .catch((error) => callback(error));

Is it possible to specify the ID when inserting it?

答案1

得分: 2

The id is specified in the object, in your case, the session object:

interface Session {
  id: string;
  // other properties
}

let session: Session = {
    id: "myId",
    // other properties
  };

this.container.items
      .create<Session>(session)
      .then(() => callback(null))
      .catch((error) => callback(error));
英文:

The id is specified in the object, in your case, the session object:

interface Session{
  id: string;
  // other properties
}

let session : Session = {
    id: &quot;myId&quot;,
    // other properties
  };

this.container.items
      .create&lt;Session&gt;(session)
      .then(() =&gt; callback(null))
      .catch((error) =&gt; callback(error));

huangapple
  • 本文由 发表于 2023年2月16日 03:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定