1

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

2
  • 1
    is there a particular reason why you want to use .RunCommand() instead of a find or aggregate query to get the CustomerInfo data in a type-safe manner? Commented Feb 19, 2020 at 8:55
  • Hi; Actually not. The reason of my usage is accesing Customerinfo from C# . I coudn't find run query as a best practice? Also there is no sample. Commented Feb 19, 2020 at 9:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.