Home » c# » .net – How to run bson command access nested items structure by using C# mongodriver client?-Exceptionshub

.net – How to run bson command access nested items structure by using C# mongodriver client?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

How can I run a simple mongodb command by accessing nested mongodb structure by writing ansi mongo script?
But it can not work because it is not correct I am looking for an answer by googling but I couldn’t find any best answer . I know using mongodb with C# it is like a torture. Please give me great advise. because I am exhausted making research on the internet. My code is a poseido code. it is a kind of my desire. Also please look at my picture to understand structure of my mongodb json structure.

  var customerinfos = db_ScaleGrid.RunCommand<BsonDocument>(new BsonDocument("db.getCollection('customers').find({'Items.0.Source.CustomerInfo':{$exists:true}})",""));
            foreach (var customerinfo in customerinfos)
            {
                var customerid = customerinfo["customerid"];
                var customerName = customerinfo["customerName"];
            }

enter image description here

How to&Answers:

i hope i’ve understood your structure and requirement correctly. if not let me know and i’ll ammend my answer accordingly. you can easily access customer info with mongodb c# driver’s AsQueryable interface like so:

var collection = new MongoClient("mongodb://localhost")
                    .GetDatabase("test")
                    .GetCollection<Customer>("customers");

var result = collection.AsQueryable()
                .Select(c => c.Items[0].Source.CustomerInfo)
                .ToArray();

here’s a strongly-typed test program:

using MongoDB.Entities;
using MongoDB.Entities.Core;
using System;
using System.Linq;

namespace StackOverFlow
{
    public class Customer : Entity
    {
        public Item[] Items { get; set; }
    }

    public class Item
    {
        public Source Source { get; set; }
    }

    public class Source
    {
        public CustomerInfo CustomerInfo { get; set; }
    }

    public class CustomerInfo
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
    }

    public static class Program
    {
        private static void Main()
        {
            new DB("test");

            var customer = new Customer
            {
                Items = new[]
                {
                    new Item
                    {
                        Source = new Source {
                            CustomerInfo = new CustomerInfo
                            {
                                CustomerID = "xxxxxxx",
                                CustomerName = "customer one"
                            }
                        }
                    }
                }
            };

            customer.Save();

            var result = DB.Queryable<Customer>()
                           .Select(c => c.Items[0].Source.CustomerInfo)
                           .ToArray();

            foreach (var info in result)
            {
                Console.WriteLine($"id: {info.CustomerName} / name: {info.CustomerName}");
            }

            Console.ReadKey();
        }
    }
}

above test program uses my library MongoDB.Entities for brevity. if you’re overwhelmed by the verbosity of the official driver, you might want to check out my library.