Mongodb 点表示法通配符?

发布于 2024-12-26 16:03:50 字数 790 浏览 0 评论 0原文

我有一组用户,每个用户都可以订阅一项或多项服务。每个服务都有一些元数据,包括用户对该服务拥有的积分数量。

如果我无法知道服务对象的密钥是什么,如何找到某些服务的积分少于 50 个的所有用户对象?

从概念上讲,它会是这样的,这是行不通的:

db.users.find({services.*.credits : {$lt : 50}})

用户集合:

   {
_id: 4f0ea25072139e4d2000001f,
services : {
    a : { credits : 100, score : 2000 },
    b : { credits : 200, score : 300 },
    c : { credits : 10, score : 1300 }
    }
},
{
_id: 4f0ea25072139e4d2000001f,
services : {
    f : { credits : 68, score : 14 },
    q : { credits : 1000, score : 102 },
    z : { credits : 59, score : 352 }
    }
}

我想要做的另一个例子,如果这里不清楚,解释如下: mongodb.org/display/DOCS/Advanced+Queries#comment-346075854" rel="noreferrer">http://www.mongodb.org/display/DOCS/Advanced+Queries#comment-346075854

I have a collection of users, each of which may be subscribed to one or more services. Each service has some meta data, including the number of credits the user has for that service.

How can I find all of the user objects who have less than 50 credits for some service if I have no way of knowing what the service objects keys will be?

Conceptually, it would be something like this, which doesn't work:

db.users.find({services.*.credits : {$lt : 50}})

The users collection:

   {
_id: 4f0ea25072139e4d2000001f,
services : {
    a : { credits : 100, score : 2000 },
    b : { credits : 200, score : 300 },
    c : { credits : 10, score : 1300 }
    }
},
{
_id: 4f0ea25072139e4d2000001f,
services : {
    f : { credits : 68, score : 14 },
    q : { credits : 1000, score : 102 },
    z : { credits : 59, score : 352 }
    }
}

Another example of what I want to do, in case it's not clear here, is explained here: http://www.mongodb.org/display/DOCS/Advanced+Queries#comment-346075854

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

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

发布评论

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

评论(3

风轻花落早 2025-01-02 16:03:50

这是对你的问题的实际答案。

如果您无法知道服务对象密钥是什么,则如何找到某些服务的积分少于 50 个的所有用户对象,如下所示。

使用 $where 查询:

db.users.find({
    $where: function () {
        for (var index in this.services)
            if (this.services[index].credits < 50)
                return this;
    }
});

This is an actual answer to your question.

How you can find all of the user objects who have less than 50 credits for some service if you have no way of knowing what the service objects keys will be is as follows.

Use a $where query:

db.users.find({
    $where: function () {
        for (var index in this.services)
            if (this.services[index].credits < 50)
                return this;
    }
});
-小熊_ 2025-01-02 16:03:50

我不知道如何使用您正在使用的架构来完成此任务。在我看来,你正在滥用对象作为数组。如果 services 是一个数组(复数提示它应该是),

db.users.find({"services.credits" : { $lt : 50 }}); 

如果您需要在单个数组元素上匹配多个条件,您可以简单地查询或使用 $elemMatch

I don't know of a way to accomplish this using the schema you're using. It seems to me you're abusing objects as arrays. If services were an array (the plural hints that it should be), you could simply query

db.users.find({"services.credits" : { $lt : 50 }}); 

or use $elemMatch if you need to match multiple conditions on a single array element.

单身情人 2025-01-02 16:03:50

我认为如果您将该服务对象放入数组中会更容易,因此您可以使用 $elemMatch,例如:

{
  services : [
    {key: "a" , credits : 100, score : 2000 },
    {key: "b", credits : 200, score : 300 },
    {key: "c", credits : 10, score : 1300 }
  ]
}

然后

{
  _id: 4f0ea25072139e4d2000001f,
  services : [
    {key: "f", credits : 68, score : 14 },
    {key: "q", credits : 1000, score : 102 },
    {key: "z", credits : 59, score : 352 }
  ]
}

您将编写的查询将如下所示:

db.coll.find({services: {$elemMatch : {credits: {$lt: 50}}}});

结果:

{ "_id" : ObjectId("4f0f2be07561bf94ea47eec4"), "services" : [  {   "key" : "a", "credits" : 100, "score" : 2000 }, { "key" : "b", "credits" : 200, "score" : 300 },    {   "key" : "c",    "credits" : 10,     "score" : 1300 } ] }

I think it would be easier if you put that services object into an array, so you can use $elemMatch, like:

{
  services : [
    {key: "a" , credits : 100, score : 2000 },
    {key: "b", credits : 200, score : 300 },
    {key: "c", credits : 10, score : 1300 }
  ]
}

and

{
  _id: 4f0ea25072139e4d2000001f,
  services : [
    {key: "f", credits : 68, score : 14 },
    {key: "q", credits : 1000, score : 102 },
    {key: "z", credits : 59, score : 352 }
  ]
}

Then the query you would write would be like this:

db.coll.find({services: {$elemMatch : {credits: {$lt: 50}}}});

result:

{ "_id" : ObjectId("4f0f2be07561bf94ea47eec4"), "services" : [  {   "key" : "a", "credits" : 100, "score" : 2000 }, { "key" : "b", "credits" : 200, "score" : 300 },    {   "key" : "c",    "credits" : 10,     "score" : 1300 } ] }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文