C# 如何在MongoDB.net中使用嵌入式字符串数组创建连接?
我有两个系列产品和类别。一个产品可以有多个类别。产品有一个字符串数组,用于将类别ID保留为name Product.Categories 我想选择具有类别详细信息的产品。 注意:我使用的是MongoDB.Net驱动程序。我可以用Linq查询来做这个吗 产品系列: ` 类别收集:C# 如何在MongoDB.net中使用嵌入式字符串数组创建连接?,c#,mongodb,C#,Mongodb,我有两个系列产品和类别。一个产品可以有多个类别。产品有一个字符串数组,用于将类别ID保留为name Product.Categories 我想选择具有类别详细信息的产品。 注意:我使用的是MongoDB.Net驱动程序。我可以用Linq查询来做这个吗 产品系列: ` 类别收集: { _id: "category_1", name : "Category 1 Name", }, { _id: "category_2", name : "Category 2 Name", } 我希
{
_id: "category_1",
name : "Category 1 Name",
},
{
_id: "category_2",
name : "Category 2 Name",
}
我希望得到如下结果:
{
_id : "product_1",
title :"Product Title 1",
categories : [
{_id = "category_1", name="Category 1 Name"},
{_id = "category_2", name="Category 2 Name"},
]
},
{
_id : "product_2",
title :"Product Title 2",
categories : [
{_id = "category_2", name="Category 2 Name"},
]
}
它基本上是一个连接。这是C端中的查找聚合。我相信您需要以下内容>
public class Category
{
public string _id { get; set; }
public string name { get; set; }
}
public class Product
{
public string _id { get; set; }
public string title { get; set; }
public string[] categories { get; set; }
}
public class AggregatedProduct
{
[BsonElement("_id")]
public string Id { get; set; }
[BsonElement("title")]
public string Title { get; set; }
[BsonElement("categories")]
public Category[] Categories { get; set; }
}
string connectionString = "mongodb://localhost:27017";
var client = new MongoClient(connectionString);
var db = client.GetDatabase("test");
var products = db.GetCollection<Product>("Products");
var categories = db.GetCollection<Category>("Categories");
var resultOfJoin = products.Aggregate().Lookup(foreignCollection: categories, localField: x => x.categories,
foreignField: x => x._id, @as: (AggregatedProduct pr) => pr.Categories).ToList();
它基本上是一个连接。这是C端中的查找聚合。我相信您需要以下内容>
public class Category
{
public string _id { get; set; }
public string name { get; set; }
}
public class Product
{
public string _id { get; set; }
public string title { get; set; }
public string[] categories { get; set; }
}
public class AggregatedProduct
{
[BsonElement("_id")]
public string Id { get; set; }
[BsonElement("title")]
public string Title { get; set; }
[BsonElement("categories")]
public Category[] Categories { get; set; }
}
string connectionString = "mongodb://localhost:27017";
var client = new MongoClient(connectionString);
var db = client.GetDatabase("test");
var products = db.GetCollection<Product>("Products");
var categories = db.GetCollection<Category>("Categories");
var resultOfJoin = products.Aggregate().Lookup(foreignCollection: categories, localField: x => x.categories,
foreignField: x => x._id, @as: (AggregatedProduct pr) => pr.Categories).ToList();
非常感谢你。。此示例解决了我的问题:再次感谢…@ntohl:我得到以下错误:聚合方法的任何重载都不接受0个参数。如何修复此问题?我有Visual Studio 2019版本16.2.3。要更新到版本16.3.6。@对于最新版本2.9.2的UNIX,仍然有一个测试代码,其中聚合没有问题。从该文件中签出,如果您缺少一个文件,那么它对我有用,但在执行products.Aggregate之前,我如何筛选我的收藏。这是我不能做的,因为如果我像这样过滤:var filter=Builders.filter.EqMyId,id;var collection=db.GetCollectionCategories;var result=collection.Findfilter。。。。。。。。我要的不是收藏,而是一张清单。列表与此处使用的聚合方法不兼容。我怎么能绕过这个?非常感谢。。此示例解决了我的问题:再次感谢…@ntohl:我得到以下错误:聚合方法的任何重载都不接受0个参数。如何修复此问题?我有Visual Studio 2019版本16.2.3。要更新到版本16.3.6。@对于最新版本2.9.2的UNIX,仍然有一个测试代码,其中聚合没有问题。从该文件中签出,如果您缺少一个文件,那么它对我有用,但在执行products.Aggregate之前,我如何筛选我的收藏。这是我不能做的,因为如果我像这样过滤:var filter=Builders.filter.EqMyId,id;var collection=db.GetCollectionCategories;var result=collection.Findfilter。。。。。。。。我要的不是收藏,而是一张清单。列表与此处使用的聚合方法不兼容。我怎么能绕过这个?