如何使用 C# mongodriver 客户端 运行 bson 命令访问嵌套项结构?
How to run bson command access nested items structure by using C# mongodriver client?
如何通过编写 ansi mongo 脚本访问嵌套的 mongodb 结构来 运行 一个简单的 mongodb 命令?
但它无法工作,因为它不正确我正在通过谷歌搜索寻找答案,但我找不到任何最佳答案。我知道在 C# 中使用 mongodb 就像一种折磨。请给我很好的建议。因为我精疲力尽地在互联网上进行研究。我的代码是poseido代码。这是我的一种愿望。另请查看我的图片以了解我的 mongodb json 结构的结构。
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"];
}
希望我已经正确理解了您的结构和要求。如果不让我知道,我会相应地修改我的答案。您可以使用 mongodb c# 驱动程序的 AsQueryable
界面轻松访问客户信息,如下所示:
var collection = new MongoClient("mongodb://localhost")
.GetDatabase("test")
.GetCollection<Customer>("customers");
var result = collection.AsQueryable()
.Select(c => c.Items[0].Source.CustomerInfo)
.ToArray();
这是一个强类型测试程序:
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System;
using System.Linq;
namespace Whosebug
{
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();
}
}
}
以上测试程序使用我的库 MongoDB.Entities 为简洁起见。如果你对官方驱动程序的冗长感到不知所措,你可能想看看我的图书馆。
如何通过编写 ansi mongo 脚本访问嵌套的 mongodb 结构来 运行 一个简单的 mongodb 命令? 但它无法工作,因为它不正确我正在通过谷歌搜索寻找答案,但我找不到任何最佳答案。我知道在 C# 中使用 mongodb 就像一种折磨。请给我很好的建议。因为我精疲力尽地在互联网上进行研究。我的代码是poseido代码。这是我的一种愿望。另请查看我的图片以了解我的 mongodb json 结构的结构。
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"];
}
希望我已经正确理解了您的结构和要求。如果不让我知道,我会相应地修改我的答案。您可以使用 mongodb c# 驱动程序的 AsQueryable
界面轻松访问客户信息,如下所示:
var collection = new MongoClient("mongodb://localhost")
.GetDatabase("test")
.GetCollection<Customer>("customers");
var result = collection.AsQueryable()
.Select(c => c.Items[0].Source.CustomerInfo)
.ToArray();
这是一个强类型测试程序:
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System;
using System.Linq;
namespace Whosebug
{
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();
}
}
}
以上测试程序使用我的库 MongoDB.Entities 为简洁起见。如果你对官方驱动程序的冗长感到不知所措,你可能想看看我的图书馆。