How do I rewrite code from creating a local file to inserting to Couchbase

I wrote a Winform application for a simple inventory management system. My _record object contains an auto-generated key/DocumentID and 10-12 other fields. (…edited to add example of _record serialized output)

[
  {
    "Key": "Office Supplies::Matron Systems::637430386232842488",
    "BarCode": "109137",
    "ItemName": "Acier Nuage",
    "ItemPartNumber": "IR-7773",
    "ItemSerialNumber": "90205974TY",
    "IsPhysicalAsset": true,
    "Location": "BVC Media",
    "Section": "Warehouse",
    "Rack": "W",
    "Shelf": "4",
    "Bin": "4",
    "Manufacturer": "Matron Systems",
    "Category": "Office Supplies",
    "Tags": [
          "Matron Systems",
          " Office Supplies",
          " Acier Nuage",
          " IR-7773"
    ],
    "RequiredAccessories": null
   }
 ]

which I serialized using Newtonsoft with:

_jsonFile = JsonConvert.SerializeObject(_record, Formatting.Indented);

Then I save as a local JSON file.
It’s time to move beyond working with a local file and move to Couchbase.

I believe the trick to moving forward is using the upsert statement, but I’m unsure how to write it. For a proof of concept I wrote a N1QL query with using a key and a couple values from my _record object.

var upsertRecord = await cluster.QueryAsync<dynamic>("UPSERT INTO `arctic-tweed-sample` (KEY, VALUE) VALUES (\"Audio::Envision Entertainment::637430386244794885\",{\"ItemName\":\"TremorEdge DH-15\",\"Manufacturer\":\"Brainlace\"})");

My question is how do I rewrite my code to parse the key as documentID, add the remaining KV pairs and Upsert the full _record object (which is output in my current application as _jsonFile) into my Couchbase bucket? Is Upsert the correct way to go? Use collection.InsertAsync? Something else?

Thanks for any suggestions.
Norm

Norm,

I would strongly recommend using collection.InsertAsync or collection.UpsertAsync rather than N1QL. The N1QL approach is great for mutating data based on query results, but if the data in question is already in your application you’ll get operation speeds an order of magnitude faster using collection methods instead of N1QL queries. It’s also much easier to write.

4 Likes

Brant:

Thanks. I used the collection.UpsertAsync for my proof of concept routine and it worked great.

Thanks again.
Norm

2 Likes