C# 驱动程序和 shell 产生不同的结果
C# driver and shell produces different result
我有两个collection; ProductStemCollections, ProductStems.
public abstract class Document : IDocument
{
[BsonId]
[BsonRepresentation( BsonType.String )]
public ObjectId Id { get; set; }
}
[BsonCollection( "ProductStemCollections" )]
public class ProductStemCollection : Document
{
public string Name { get; set; }
[BsonRepresentation( BsonType.String )]
public List<ObjectId> ProductStems { get; set; } = new List<ObjectId>();
}
[BsonCollection( "ProductStems" )]
public class ProductStem : Document
{
[BsonRepresentation(BsonType.String)]
public ObjectId PromotedProductId { get; set; }
public string ProductCode { get; set; }
}
我想聚合词干 collection 中列出的词干,如下所示:
var query = prodStemCollectionRepo.Collection
.Aggregate()
.Lookup( "ProductStems", "ProductStems", "_id", "stems" );
var strQuery = query.ToString();
var results = await query.ToListAsync();
这将产生查询
db.ProductStemCollections.aggregate([{ "$lookup" : { "from" : "ProductStems", "localField" : "ProductStems", "foreignField" : "_id", "as" : "stems" } }])
如果我 运行 在 Cosmos 数据库中进行此查询 shell,我将得到所需的结果:
{
"_id" : "61694434ba77dc8362d56858",
"Name" : "Sweden",
"ProductStems" : [
"61694434ba77dc8362d56859",
"61694434ba77dc8362d5685b",
"61694435ba77dc8362d5685d",
"61694435ba77dc8362d5685f",
"61694436ba77dc8362d56861",
"61694438ba77dc8362d56863"
],
"stems" : [
{
"_id" : "61694434ba77dc8362d56859",
"PromotedProductId" : "61694434ba77dc8362d5685a",
"ProductCode" : "P1"
}
...
]
}
但是,运行使用上面的 C# 代码不会产生相同的结果,即使查询是相同的数组“stems”丢失:
{
{
"_id": "61694434ba77dc8362d56858",
"Name": "Sweden",
"ProductStems": [
"61694434ba77dc8362d56859",
"61694434ba77dc8362d5685b",
"61694435ba77dc8362d5685d",
"61694435ba77dc8362d5685f",
"61694436ba77dc8362d56861",
"61694438ba77dc8362d56863"
]
}
}
我错过了什么?
好的,所以我最终转向 Mongo Db Atlas。创建了一个集合 class,其中包含一个词干集合及其词干:
public class AggregatedStemCollection : Document
{
public string? Name { get; set; }
[BsonRepresentation(BsonType.String)]
public List<ObjectId> ProductStems { get; set; } = new List<ObjectId>();
public List<ProductStem> Stems { get; set; } = new List<ProductStem>();
}
并修改了林德查询:
var query = prodStemCollectionRepo.Collection
.Aggregate()
.Match( c => c.Name == "Sweden" )
.Lookup(
foreignCollection: productStemRepo.Collection,
localField: x => x.ProductStems,
foreignField: y => y.Id,
@as: ( AggregatedStemCollection asc ) => asc.Stems );
var results = query.ToList();
我有两个collection; ProductStemCollections, ProductStems.
public abstract class Document : IDocument
{
[BsonId]
[BsonRepresentation( BsonType.String )]
public ObjectId Id { get; set; }
}
[BsonCollection( "ProductStemCollections" )]
public class ProductStemCollection : Document
{
public string Name { get; set; }
[BsonRepresentation( BsonType.String )]
public List<ObjectId> ProductStems { get; set; } = new List<ObjectId>();
}
[BsonCollection( "ProductStems" )]
public class ProductStem : Document
{
[BsonRepresentation(BsonType.String)]
public ObjectId PromotedProductId { get; set; }
public string ProductCode { get; set; }
}
我想聚合词干 collection 中列出的词干,如下所示:
var query = prodStemCollectionRepo.Collection
.Aggregate()
.Lookup( "ProductStems", "ProductStems", "_id", "stems" );
var strQuery = query.ToString();
var results = await query.ToListAsync();
这将产生查询
db.ProductStemCollections.aggregate([{ "$lookup" : { "from" : "ProductStems", "localField" : "ProductStems", "foreignField" : "_id", "as" : "stems" } }])
如果我 运行 在 Cosmos 数据库中进行此查询 shell,我将得到所需的结果:
{
"_id" : "61694434ba77dc8362d56858",
"Name" : "Sweden",
"ProductStems" : [
"61694434ba77dc8362d56859",
"61694434ba77dc8362d5685b",
"61694435ba77dc8362d5685d",
"61694435ba77dc8362d5685f",
"61694436ba77dc8362d56861",
"61694438ba77dc8362d56863"
],
"stems" : [
{
"_id" : "61694434ba77dc8362d56859",
"PromotedProductId" : "61694434ba77dc8362d5685a",
"ProductCode" : "P1"
}
...
]
}
但是,运行使用上面的 C# 代码不会产生相同的结果,即使查询是相同的数组“stems”丢失:
{
{
"_id": "61694434ba77dc8362d56858",
"Name": "Sweden",
"ProductStems": [
"61694434ba77dc8362d56859",
"61694434ba77dc8362d5685b",
"61694435ba77dc8362d5685d",
"61694435ba77dc8362d5685f",
"61694436ba77dc8362d56861",
"61694438ba77dc8362d56863"
]
}
}
我错过了什么?
好的,所以我最终转向 Mongo Db Atlas。创建了一个集合 class,其中包含一个词干集合及其词干:
public class AggregatedStemCollection : Document
{
public string? Name { get; set; }
[BsonRepresentation(BsonType.String)]
public List<ObjectId> ProductStems { get; set; } = new List<ObjectId>();
public List<ProductStem> Stems { get; set; } = new List<ProductStem>();
}
并修改了林德查询:
var query = prodStemCollectionRepo.Collection
.Aggregate()
.Match( c => c.Name == "Sweden" )
.Lookup(
foreignCollection: productStemRepo.Collection,
localField: x => x.ProductStems,
foreignField: y => y.Id,
@as: ( AggregatedStemCollection asc ) => asc.Stems );
var results = query.ToList();