我是mongoDB的新手,我无法弄清楚如何从集合中的对象中提取少量字段。我有一个名为recipe的集合,如下所示: -
<ListView x:Name="listView" ItemsSource="{x:Bind vm.Tables}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="mdl:TableInfo">
<TextBlock Text="{x:Bind Name}" FontSize="25" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView x:Name="listViewTable"
ItemsSource="{Binding ElementName=listView, Path=SelectedItem.Fields}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="mdl:FieldInfo">
<TextBlock Text="{x:Bind Name}" FontSize="25" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我想在访问给定路线时仅提取两个食谱的标题和ID。目前,我正在使用这个函数,它返回两个食谱的所有字段。
[
{
"_id": "b44e864d-de8f-4110-b676-bbee13417b2e",
"title": "Fried Eggs",
"ingredients": [
{
"name": "Eggs",
"amount": "10"
},
{
"name": "Olive Oil",
"amount": "2 tbs"
}
],
"steps": [
"1. Heat the pan",
"2.Add Olive Oil",
"3. Add Eggs",
"4. Saute"
],
"comments": [
{
"_id": "8604e67c-2426-4742-bc91-25ee1c4064b5",
"poster": "Ron Swanson",
"comment": "Wrong instructions, Got stuck in Toaster"
},
{
"_id": "040412bc-9046-49ca-9a65-19151b32cd91",
"poster": "Tom Haverford",
"comment": "What are Eggs?"
},
{
"_id": "1034f082-b802-4382-be3e-ef50f07a530a",
"poster": "Andy Dwyer",
"comment": "What came firsst, Eggs or Chicken"
}
]
},
{
"_id": "da6f9798-6547-42f5-85ed-043efabeb196",
"title": "Boiled Eggs",
"ingredients": [
{
"name": "Eggs",
"amount": "5"
},
{
"name": "Water",
"amount": "3 Cups"
}
],
"steps": [
"1.Boil the Water",
"2.Add Eggs",
"3. Keepit for 5 mins",
"4. Let it Cool Down",
"5. Peel off the shell"
],
"comments": [
{
"_id": "cc569ebb-1307-499a-b9ee-7b24e557859f",
"poster": "Ron Swanson",
"comment": "Wrong instructions, Got stuck in Oven"
},
{
"_id": "4f02756a-6e1b-4bda-a928-b3c5e03b55d8",
"poster": "Leslie Knope",
"comment": "What are Eggs?"
},
{
"_id": "0f34a5cc-ebe3-41b5-8f0b-6d1a3c206e6b",
"poster": "Andy Dwyer",
"comment": "Can I remove the shell first and then boil?"
}
]
}]
我应该使用什么查询来提取两个食谱的标题和ID而不是所有细节?
答案 0 :(得分:1)
根据我的理解,您正在尝试从mongo集合中获取特定字段。您只需使用投影即可实现此目的。
VariableNode
有关更多信息,请按以下步骤操作 https://docs.mongodb.com/v3.2/tutorial/project-fields-from-query-results/#return-the-specified-fields-and-the-id-field-only