Save File in DocumentDb with custom name - azure

I'm trying to save a XML file in DocumentDb in Json format. I have no problem in converting it and saving. All the conversion and saving are working fine.
However when I store my Xml file the DocumentDB provides its own file name to the file. Eg; 8756b9b9-41ac-47ca-9f4c-abec60b318be.
But I want to save the file with my own custom name Eg; MyXmlFile1 or MyXmlFile2 etc;
How do I pass my custom name when saving the file? That is MyXmlFile1 or MyXmlFile2.
"jsontoStore" has the content of the file I want to store into DocumentDB.
Code1: Storing without passing any file name.
await client.CreateDocumentAsync(documentCollection.SelfLink, jsontoStore);
Code2: Storing with custom file name but here I'm not able to pass my content that is "jsontostore"
document = await client.CreateDocumentAsync(documentCollection.SelfLink,
new Document
{
Id = "sample1"
});

I believe you are referring to the id of a document, since you mentioned the auto-generated GUID.
All documents must have an unique value in the id property (it is the primary key for the document). If an id is not provided at the time of document creation, DocumentDB will generate one for you.
The solution here is to simply set the id field with the name of your choice (e.g. MyXmlFile1, MyXmlFile2) after you convert your XML to JSON.

Related

how to keep file upload field url in single-line field

I have a form with two fields:
File upload field
Single-line field
I want to save the URL of the uploaded file in the single-line text field. I will also have this address for the file storage because I need that address. How should I do this?
To do this, I first created a new storage path using the gform_upload_path function, but I don't know how to insert this new path into the single-line text field.
please help me and write the code

Override id creation in Azure cosmos DB

I have a JSON document, which I need to push to azure cosmos db. I am using DocumentDB data migration tool to dump my json file to azure cosmos DB. The problem is my JSON contains an id field which is same for all the records. For e,g.
"angularAcceleration_T12_x:-0.137993,"id":"5946001","jointAngle_jRightHip_z"
And because of this id field, I am not able to insert more than 1 record in my collection as after inserting one record I get an error message "resource with specified id already exist".
Is there a way to force cosmos DB to ignore the id field in JSON and create a GUID for each entry, like it does if it does not find an id field.
Thanks,
Yaju
After a lot of searching i came to the verdict that if you have a column named "id"(case sensitive) in your json, cosmos db will be make that column value as the id for the document, you can not use any other column as the unique identifier.
If cosmos does not find id field, it will create a GUID as the id for the document.
Microsoft.Azure.Documents.Resource.Id documentation hints that it is serialised in JSON with fixed name "id":
[Newtonsoft.Json.JsonProperty(PropertyName="id")]
public virtual string Id { get; set; }
So, I would say that you cannot use another property as storage PK.
What you CAN do, is convert your JSON document yourself, for example renaming your current id to originalId or similar. Then documentDB would generate unique ids on import for you.

How to prevent saving same named file on CouchDb?

I am using CouchDB with Divan - C# interfacing library for CouchDb.
A file can be uploaded many times on CouchDb. Every time the "id" is changed after file is uploaded, but the "rev" remains the same.
This happens even if all custom attributes defined for file being uploaded are same any existing file on CouchDb with same name.
Is there any way that can avoid uploading same named file if all custom attributes are same? Fetching all files and checking them for file name repetition could be a way, but definitely not preferable for its required time depending on other factors.
Thanking you.
Let's say you have 3 attributes for a file :
name
size in bytes
Date of modification
I see two main possibilities to avoid duplicates in your database.
Client approach
You query the database to check if the document with the same attributes exists with a view. If it's not existing, create it.
User defined id
You could generate an id from the attributes as this library is doing.
For example, if my document has those attributes :
"name":"test.txt",
"size":"512",
"lastModified":"2016-11-08T15:44:29.563Z"
You could build a unique id like this :
"_id":"test.txt/2016-11-08T15:44:29.563Z/512"

How to store files in Couchbase database using Node.JS?

I have a JSON document having some properties including attachment(abc.txt).
How to save attachments in couchbase using Node.JS?
Take a look at the Couchbase Node.JS SDK
You have two choices in how to store the attachment:
Inline with the document. If the attachment is small and/or easily embedded into JSON (the attachment is valid JSON itself) then you can simply add a element to your JSON document containing the attachment body, for example:
{attachment_name: "abc.txt",
attachment_body: "The quick brown fox jumps over the lazy dog\n"
...
}
As a separate document, referenced from the first. If the document is large / you don't want to serialize it inline, then create a field which just refers to the key of the actual attachment:
{attachment_name: "abc.txt",
attachment_ref: "attachment::document1_attachment1"
...
}
Then you'd have a second document named attachment::document1_attachment which was the actual attachment document.

Array of attachment type - how to get a filename for highlighted fragment?

I use ElasticSearch to index resources. I create document for each indexed resource. Each resource can contain meta-data and an array of binary files. I decided to handle these binary files with attachment type. Meta-data is mapped to simple fields of string type. Binary files are mapped to array field of attachment type (field named attachments). Everything works fine - I can find my resources based on contents of binary files.
Another ElasticSearch's feature I use is highlighting. I managed to successfully configure highlighting for both meta-data and binary files, but...
When I ask for highlighted fragments of my attachments field I only get fragments of these files without any information about source of the fragment (there are many files in attachment array field). I need mapping between highlighted fragment and element of attachment array - for instance the name of the file or at least the index in array.
What I get:
"attachments" => ["Fragment <em>number</em> one", "Fragment <em>number</em> two"]
What I need:
"attachments" => [("file_one.pdf", "Fragment <em>number</em> one"), ("file_two.pdf", "Fragment <em>number</em> two")]
Without such mapping, the user of application knows that particular resource contains files with keyword but has no indication about the name of the file.
Is it possible to achieve what I need using ElasticSearch? How?
Thanks in advance.
So what you want here is to store the filename.
Did you send the filename in your json document? Something like:
{
"my_attachment" : {
"_content_type" : "application/pdf",
"_name" : "resource/name/of/my.pdf",
"content" : "... base64 encoded attachment ..."
}
}
If so, you can probably ask for field my_attachment._name.
If it's not the right answer, can you refine a little your question and give a JSON sample document (without the base64 content) and your mapping if any?
UPDATE:
When it come from an array of attachments you can't get from each file it comes because everything is flatten behind the scene. If you really need that, you may want to have a look at nested fields instead.

Resources