如何与“ project”在MongoDB查询中

发布于 2025-01-27 08:50:50 字数 881 浏览 3 评论 0原文

这是我数据的一个非常简单的示例:

[
  { _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2025-02-03 08:50:50

您需要一个投影sort。有时,很难在C#语法中写入,但是您可以使用bsondocument允许使用mongoDB查询语法。

ProjectionDefinition<BsonDocument> projection = new BsonDocument
{
    { "category", 1 },
    { "title", 1 },
    { "priority",
        new BsonDocument(
            "$eq", new BsonArray
            {
                "$_id",
                2
            }
        )
    }
};
SortDefinition<BsonDocument> sort = Builders<BsonDocument>.Sort.Descending("priority");

var result = await collection.Aggregate()
    .Match(filterDefinition)
    .Project(projection)
    .Sort(sort)
    .ToListAsync();

如果要返回具体类型的列表,例如list&lt; item&gt;

public class Item
{
    [BsonId]
    public int Id { get; set; }
    [BsonElement("category")]
    public string Category { get; set; }
    [BsonElement("title")]
    public string Title { get; set; }
    [BsonElement("priority")]
    public bool Priority { get; set; }
}
var collection = _db.GetCollection<Item>("item");

ProjectionDefinition<Item> projection = new BsonDocument
{
    { "category", 1 },
    { "title", 1 },
    { "priority",
        new BsonDocument(
            "$eq", new BsonArray
            {
                "$_id",
                2
            }
        )
    }
};
SortDefinition<Item> sort = Builders<Item>.Sort.Descending("priority");

var result = await collection.Aggregate()
    .Match(filterDefinition)
    .Project<Item>(projection)
    .Sort(sort)
    .ToListAsync();

输出

You need a projection and sort. Sometimes, it is hard to write in C# syntax, but you can work with BsonDocument which allows using MongoDB query syntax.

ProjectionDefinition<BsonDocument> projection = new BsonDocument
{
    { "category", 1 },
    { "title", 1 },
    { "priority",
        new BsonDocument(
            "$eq", new BsonArray
            {
                "$_id",
                2
            }
        )
    }
};
SortDefinition<BsonDocument> sort = Builders<BsonDocument>.Sort.Descending("priority");

var result = await collection.Aggregate()
    .Match(filterDefinition)
    .Project(projection)
    .Sort(sort)
    .ToListAsync();

If you are returning a List of concrete type such as List<Item>,

public class Item
{
    [BsonId]
    public int Id { get; set; }
    [BsonElement("category")]
    public string Category { get; set; }
    [BsonElement("title")]
    public string Title { get; set; }
    [BsonElement("priority")]
    public bool Priority { get; set; }
}
var collection = _db.GetCollection<Item>("item");

ProjectionDefinition<Item> projection = new BsonDocument
{
    { "category", 1 },
    { "title", 1 },
    { "priority",
        new BsonDocument(
            "$eq", new BsonArray
            {
                "$_id",
                2
            }
        )
    }
};
SortDefinition<Item> sort = Builders<Item>.Sort.Descending("priority");

var result = await collection.Aggregate()
    .Match(filterDefinition)
    .Project<Item>(projection)
    .Sort(sort)
    .ToListAsync();

Output

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文