Return 集合中的子文档类型
Return subdocument type from collection
(我是 mongodb 的新手)
假设集合 Entities
为:
{
"_id":...,
"name":"Entity1",
"properties": {
"property1":{
"name:"name1",
"value:"value1"
},
{
"property2":{
"name:"name2",
"value:"value2"
},
{
"property3":{
"name:"name3",
"value:"value3"
}
}
},
{
"_id":...,
"name":"Entity2",
"properties": {
"property1":{
"name:"name1",
"value:"value1"
},
{
"property2":{
"name:"name2",
"value:"value2"
},
{
"property3":{
"name:"name3",
"value:"value3"
}
}
}
在 C# 模型中:
public class Entity {
public ObjectId _id;
public string Name;
public Dictionary<string, Property> Properties;
}
我想使用 c# mongodb linq 从 Entity1
.
到 return property2
(Property
类型)
我知道如何在 shell 中查询它,但我的问题是无论我尝试在 c# linq 中查询实体集合的方式如何,它似乎总是 return 实体。无法找到 returns 子类型的方法。
可以吗?
PS。
只是为了强调它是一个 return 类型的问题而不是查询,我可以在对象内重复 "property2" 键以供查询。
您可以使用下面的查找查询。
var result = collection.
Find(x => x.Name == "Entity1").
Project(p => p.Properties.Single(s => s.Key == "property2")).
Single(s => s.Key == "property2").Value;
(我是 mongodb 的新手)
假设集合 Entities
为:
{
"_id":...,
"name":"Entity1",
"properties": {
"property1":{
"name:"name1",
"value:"value1"
},
{
"property2":{
"name:"name2",
"value:"value2"
},
{
"property3":{
"name:"name3",
"value:"value3"
}
}
},
{
"_id":...,
"name":"Entity2",
"properties": {
"property1":{
"name:"name1",
"value:"value1"
},
{
"property2":{
"name:"name2",
"value:"value2"
},
{
"property3":{
"name:"name3",
"value:"value3"
}
}
}
在 C# 模型中:
public class Entity {
public ObjectId _id;
public string Name;
public Dictionary<string, Property> Properties;
}
我想使用 c# mongodb linq 从 Entity1
.
property2
(Property
类型)
我知道如何在 shell 中查询它,但我的问题是无论我尝试在 c# linq 中查询实体集合的方式如何,它似乎总是 return 实体。无法找到 returns 子类型的方法。
可以吗?
PS。 只是为了强调它是一个 return 类型的问题而不是查询,我可以在对象内重复 "property2" 键以供查询。
您可以使用下面的查找查询。
var result = collection.
Find(x => x.Name == "Entity1").
Project(p => p.Properties.Single(s => s.Key == "property2")).
Single(s => s.Key == "property2").Value;