如何与“ project”在MongoDB查询中
这是我数据的一个非常简单的示例:
[
{ _id: 1,
title: 'other item',
category: 'show'
},
{ _id: 2,
title: 'THE ITEM THAT MUST BE RETURNED FIRST',
category: 'show'
},
{ _id: 3,
title: 'other item 2',
category: 'show'
},
{ _id: 4,
title: 'item not matching query',
category: 'hide'
}
]
我正在尝试将ID与顶部的特定数字进行优先排序,并在MongoDB中尝试了相同的IT:
itemsCollection.aggregate([
{
"$project": {
"_id": 1,
"category": 1,
"title": 1,
"priority": {
"$eq": [
"$_id",
2
]
}
}
},
{
"$sort": {
"priority": -1
}
}
])
我该如何在C#中使用centregate在C#中尝试相同的方法:
var aggregation = await collection.Aggregate()
.Match(filterDefinition)
.Facet(countFacet, dataFacet)
.ToListAsync();
Here is a very simplified example of my data:
[
{ _id: 1,
title: 'other item',
category: 'show'
},
{ _id: 2,
title: 'THE ITEM THAT MUST BE RETURNED FIRST',
category: 'show'
},
{ _id: 3,
title: 'other item 2',
category: 'show'
},
{ _id: 4,
title: 'item not matching query',
category: 'hide'
}
]
I am trying to prioritize Id with the specific number at the top with sorting, had tried the same by in MongoDB:
itemsCollection.aggregate([
{
"$project": {
"_id": 1,
"category": 1,
"title": 1,
"priority": {
"$eq": [
"$_id",
2
]
}
}
},
{
"$sort": {
"priority": -1
}
}
])
How can I try the same in C# using aggregate:
var aggregation = await collection.Aggregate()
.Match(filterDefinition)
.Facet(countFacet, dataFacet)
.ToListAsync();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个
投影
和sort
。有时,很难在C#语法中写入,但是您可以使用bsondocument
允许使用mongoDB查询语法。如果要返回具体类型的列表,例如
list< item>
,You need a
projection
andsort
. Sometimes, it is hard to write in C# syntax, but you can work withBsonDocument
which allows using MongoDB query syntax.If you are returning a List of concrete type such as
List<Item>
,