我可以使用基于 ID link 在 Azure DocumentDb 中使用服务器端 Javascript 读取文档吗?
Can I use ID-based link to read document using Server-side Javascript in Azure DocumentDb?
我正在 JavaScript 中编写服务器端存储过程以将文档批量插入到我的 documentDb 集合中。我还想在插入主文档后更新元数据文档。我已经知道我的元数据文档的 ID。
根据我的研究,我知道不是使用:
collection.queryDocuments(collectionLink, 'SELECT * FROM root r WHERE r.id = "' + metadataDocId + '"', callback)
我应该使用:
collection.readDocument(documentLink, options, callback)
我的问题是,我可以使用 ID-based document link(因为我知道所有 ID)还是只能使用 self link?官方文档没有指定任何内容。
我的问题的原因是 C# 客户端 API 过去只支持 self-link,但现在它也支持基于 ID 的 link。这种支持是否也扩展到 javascript API?
ID-based links 尽管文档中不够清晰,但应该可以正常工作。
是的,可以。在JavaScript API, Collection.getAltLink()
is convenient for getting the Id-based link of the collection. You can append the document Id to that using the format described in Azure DocumentDB bids fond farewell to Self-Links。在下面的示例中,__
是 getContext().getCollection()
.
的别名
var link = __.getAltLink() + "/docs/" + id;
现在您可以使用 link
进行带有文档 link 参数的操作:
readDocument()
replaceDocument()
deleteDocument()
createAttachment()
queryAttachments()
readAttachments()
upsertAttachment()
readDocument
的示例:
__.readDocument(link, {}, function (err, resource, options) {
// callback code ...
});
我正在 JavaScript 中编写服务器端存储过程以将文档批量插入到我的 documentDb 集合中。我还想在插入主文档后更新元数据文档。我已经知道我的元数据文档的 ID。 根据我的研究,我知道不是使用:
collection.queryDocuments(collectionLink, 'SELECT * FROM root r WHERE r.id = "' + metadataDocId + '"', callback)
我应该使用:
collection.readDocument(documentLink, options, callback)
我的问题是,我可以使用 ID-based document link(因为我知道所有 ID)还是只能使用 self link?官方文档没有指定任何内容。
我的问题的原因是 C# 客户端 API 过去只支持 self-link,但现在它也支持基于 ID 的 link。这种支持是否也扩展到 javascript API?
ID-based links 尽管文档中不够清晰,但应该可以正常工作。
是的,可以。在JavaScript API, Collection.getAltLink()
is convenient for getting the Id-based link of the collection. You can append the document Id to that using the format described in Azure DocumentDB bids fond farewell to Self-Links。在下面的示例中,__
是 getContext().getCollection()
.
var link = __.getAltLink() + "/docs/" + id;
现在您可以使用 link
进行带有文档 link 参数的操作:
readDocument()
replaceDocument()
deleteDocument()
createAttachment()
queryAttachments()
readAttachments()
upsertAttachment()
readDocument
的示例:
__.readDocument(link, {}, function (err, resource, options) {
// callback code ...
});