Mongo:从放置在对象中的数组中选择X个元素

发布于 2024-12-07 11:27:11 字数 1220 浏览 0 评论 0原文

我在 MongoDB 中有以下用户集合:

{
  "_id" : 1,
  "facebook_id" : XX,
  "name": "John Doe",
  "points_snapshot" : [{
      "unix_timestamp" : 1312300552,
      "points" : 115
    }, {
      "unix_timestamp" : 1312330380,
      "points" : 110
    }, {
      "unix_timestamp" : 1312331610,
      "points" : 115
    }]
}

是否可以编写一个查询,通过该查询我可以获取 id 1 的用户以及特定一天后的快照。例如:1312330300?

基本上将快照限制为符合某些条件的 X 个数量?

到目前为止,我已经尝试过 C#:

Query.And(
 Query.EQ("facebook_id", XX), 
 Query.GTE("points_snapshot.unix_timestamp", sinceDateTimeStamp)))
 .SetFields("daily_points_snapshot", "facebook_id")

我很快意识到它无法满足我的需求。

任何帮助将不胜感激! 谢谢!

编辑:我的解决方案

如果有人希望快速解决此问题,这就是我最终所做的:

var points = MyDatabase[COLLECTION_NAME]
                .Find(Query.EQ("facebook_id", XX))
                .SetFields("points_snapshot", "facebook_id")
                .FirstOrDefault();

if (points != null) {
        var pointsArray = points.AsBsonDocument["points_snapshot"].AsBsonArray
        .Where(x => x.AsBsonDocument["unix_timestamp"].AsInt32 > sinceDateTimeStamp)
        .ToList();

        return pointsArray;
}

I have the following collection for a user in a MongoDB:

{
  "_id" : 1,
  "facebook_id" : XX,
  "name": "John Doe",
  "points_snapshot" : [{
      "unix_timestamp" : 1312300552,
      "points" : 115
    }, {
      "unix_timestamp" : 1312330380,
      "points" : 110
    }, {
      "unix_timestamp" : 1312331610,
      "points" : 115
    }]
}

Is it possible to write one query which by which I can get the user for id 1 along with the snapshots after a particular day. eg: 1312330300?

Basically limit the snapshots to X numbers matching some criteria?

So far, I have tried in C#:

Query.And(
 Query.EQ("facebook_id", XX), 
 Query.GTE("points_snapshot.unix_timestamp", sinceDateTimeStamp)))
 .SetFields("daily_points_snapshot", "facebook_id")

which I soon realised will not work for what I want.

Any help will be appreciated!
Thanks!

EDIT: My Solution to this

If anyone is looking to get a quick fix to this, this is what I ended up doing:

var points = MyDatabase[COLLECTION_NAME]
                .Find(Query.EQ("facebook_id", XX))
                .SetFields("points_snapshot", "facebook_id")
                .FirstOrDefault();

if (points != null) {
        var pointsArray = points.AsBsonDocument["points_snapshot"].AsBsonArray
        .Where(x => x.AsBsonDocument["unix_timestamp"].AsInt32 > sinceDateTimeStamp)
        .ToList();

        return pointsArray;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

别靠近我心 2024-12-14 11:27:11

是否可以编写一个查询,通过该查询我可以获取 id 1 的用户以及特定一天之后的快照。例如:1312330300?

不可能。

这里的问题是 MongoDB 查询返回所有匹配的文档。您的文档包含一个对象数组,但 MongoDB 不会将这些视为“文档”。

现在,您可以限制返回的字段,但在您的情况下,您实际上想要限制返回的“数组的部分”。

这是一个长期悬而未决的问题,JIRA 票证位于此处。这张罚单的日期是大约 18 个月前,并且不断被推迟。因此,如果此类查询对您的架构至关重要,您可能必须重新设计架构。

Is it possible to write one query which by which I can get the user for id 1 along with the snapshots after a particular day. eg: 1312330300?

Not possible.

The problem here is that MongoDB queries return all matching Documents. Your Document contains an array of objects, but MongoDB does not treat these as "Documents".

Now, you can limit the fields that are returned, but in your case you actually want to limit the "portions of an array" that are returned.

This is a long-outstanding issue, the JIRA ticket is here. The ticket is dated about 18 months ago and it continues to get pushed back. So if this type of query is critical to your architecture, you may have to re-design the architecture.

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