How to insert a string array values record in couch base bucket?

How to insert a dynamic model record in couch base bucket? I have string array columns and string array values. Need to insert this record, we need convert the model those records and assign to the Content Property. Any possibilities are there for inserting this records without model Creation.

Hi @balakrishnanvaluesol,

This seems more like a C# question than a Couchbase one, since any object you set as the Content will be serialized into JSON.

But, I’ll take a shot at one solution for you. I’m going to assume that you have two arrays of the same exact size. One array contains field names (as strings), the other contains values (which could be anything, but I’ll assume they’re also strings for this example).

using System;
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json;

namespace deleteme
{
    class Program
    {
        static void Main(string[] args)
        {
            var fieldNames = new[] { "name", "shoeSize", "twitter" };
            var values = new[] { "Matt", "13", "@mgroves" };

            var obj = new ExpandoObject();
            for (int i = 0; i < fieldNames.Length; i++)
                obj.TryAdd(fieldNames[i], values[i]);

            Console.WriteLine(JsonConvert.SerializeObject(obj));
        }
    }
}

The output of that program will be:
{"name":"Matt","shoeSize":"13","twitter":"@mgroves"}

So, when using Couchbase, you would set Content to obj. I have no idea if there are better solutions out there, but that seems to work.

i need some sample for update and delete using c#…

@balakrishnanvaluesol, I’ve created a couple of videos to help you get started with ASP.NET or ASP.NET Core, both which use C# - https://blog.couchbase.com/asp-net-core-couchbase-getting-started/ and https://blog.couchbase.com/asp-net-couchbase-getting-started/

Also check out the documentation to Start Using the SDK: https://developer.couchbase.com/documentation/server/current/sdk/dotnet/start-using-sdk.html

The simplest update (update meaning a document already exists and you want to change it) with C# building on the example from earlier in this thread:

using System;
using System.Collections.Generic;
using System.Dynamic;
using Couchbase;
using Couchbase.Configuration.Client;

namespace deleteme
{
    class Program
    {
        static void Main(string[] args)
        {
            // the arrays you want to make into a dynamic object
            var fieldNames = new[] { "name", "shoeSize", "twitter" };
            var values = new[] { "Matt", "13", "@mgroves" };

            // create a dynamic object with the arrays
            var obj = new ExpandoObject();
            for (int i = 0; i < fieldNames.Length; i++)
                obj.TryAdd(fieldNames[i], values[i]);

            // connect to a couchbase cluster
            var cluster = new Cluster(new ClientConfiguration
            {
                Servers = new List<Uri> {new Uri("http://localhost:8091")}
            });
            cluster.Authenticate("matt", "password");
            var bucket = cluster.OpenBucket("mybucket");

            // create "empty" document
            bucket.Insert("my-document-key", new { thisobject = "isempty" });
            Console.WriteLine("Created a document. Press a key to continue.");
            Console.ReadKey();

            // update document
            bucket.Replace("my-document-key", obj);
            Console.WriteLine("Updated the document. Press a key to continue.");
            Console.ReadKey();

            // delete document
            bucket.Remove("my-document-key");
            Console.WriteLine("Deleted the document. Press a key to continue.");
            Console.ReadKey();
        }
    }
}

thanks for your support…and insert code worked fine…

1 Like

i am trying to insert data’s to bucket from another source via ExpandoObject(); but i am not able to get all the data’s in the source data …i will get only few data’s into the bucket …how i can insert all the data’s to the bucket.if i use query to insert data it will give the exact result for me ?

this is the code i am using…

for (int z = 0; z < destColumnList.Length; z++)
{
test.Add(destColumnList[z], rowdata[z]);

                            }
                            var dynamicObject = new ExpandoObject() as IDictionary<string, Object>;
                            foreach (var property in test)
                            {
                                dynamicObject.Add(property.Key, property.Value);
                            }

var doc = new Document { Id = tableName + “_” + myRndID, Content = dynamicObject };
var result = bucket.Insert(doc);

send the sample with query based…

Hi again @balakrishnanvaluesol,

Based on what you’re showing me, I’m not sure what the problem is. I don’t know what you have in destColumnList or rowdata. I would recommend debugging through the code step by step to see if dynamicObject is what you expect it to be.

Also, if you are pulling data from some other source and directly inserting it into Couchbase, my guess is the use of ExpandoObject is probably more work than you need to do. If you’re pulling from a relational database, you might be better off exporting to csv and then using something like cbimport.