在Mongo中存储查询 [英] Storing a query in Mongo

查看:80
本文介绍了在Mongo中存储查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这种情况:一个网上商店,我要根据一组参数配置应该在sjop中列出哪些项目. 我希望它是可配置的,因为这使我可以尝试不同的参数,也可以轻松更改其值.

This is the case: A webshop in which I want to configure which items should be listed in the sjop based on a set of parameters. I want this to be configurable, because that allows me to experiment with different parameters also change their values easily.

我有一个要基于多个参数查询的产品集合. 在这里可以找到其中的几个:

I have a Product collection that I want to query based on multiple parameters. A couple of these are found here:

产品内:

"delivery" : {
    "maximum_delivery_days" : 30,
    "average_delivery_days" : 10,
    "source" : 1,
    "filling_rate" : 85,
    "stock" : 0
}

但是还存在其他参数.

用于确定是否包含产品的此类查询的示例可能是:

An example of such query to decide whether or not to include a product could be:

"$or" : [
        {
            "delivery.stock" : 1
        },
        {
            "$or" : [
                    {
                            "$and" : [
                                    {
                                            "delivery.maximum_delivery_days" : {
                                                    "$lt" : 60
                                            }
                                    },
                                    {
                                            "delivery.filling_rate" : {
                                                    "$gt" : 90
                                            }
                                    }
                            ]
                    },
                    {
                            "$and" : [
                                    {
                                            "delivery.maximum_delivery_days" : {
                                                    "$lt" : 40
                                            }
                                    },
                                    {
                                            "delivery.filling_rate" : {
                                                    "$gt" : 80
                                            }
                                    }
                            ]
                    },
                    {
                            "$and" : [
                                    {
                                            "delivery.delivery_days" : {
                                                    "$lt" : 25
                                            }
                                    },
                                    {
                                            "delivery.filling_rate" : {
                                                    "$gt" : 70
                                            }
                                    }
                            ]
                    }
            ]
        }
]

现在要使其可配置,我需要能够处理布尔逻辑,参数和值. 因此,我有了主意,因为此类查询本身是JSON,因此将其存储在Mongo中并让我的Java应用程序检索它. 接下来的事情是在过滤器中使用它(例如查找或其他)并处理相应的产品选择. 这种方法的优点是,我可以在程序外部实际分析数据和查询的有效性.

Now to make this configurable, I need to be able to handle boolean logic, parameters and values. So, I got the idea, since such query itself is JSON, to store it in Mongo and have my Java app retrieve it. Next thing is using it in the filter (e.g. find, or whatever) and work on the corresponding selection of products. The advantage of this approach is that I can actually analyse the data and the effectiveness of the query outside of my program.

我会按名称将其存储在数据库中.例如.

I would store it by name in the database. E.g.

{ 
    "name": "query1",
    "query": { the thing printed above starting with "$or"... }
}

使用:

db.queries.insert({
    "name" : "query1",
    "query": { the thing printed above starting with "$or"... }
})

这将导致:

2016-03-27T14:43:37.265+0200 E QUERY    Error: field names cannot start with $ [$or]
    at Error (<anonymous>)
    at DBCollection._validateForStorage (src/mongo/shell/collection.js:161:19)
    at DBCollection._validateForStorage (src/mongo/shell/collection.js:165:18)
    at insert (src/mongo/shell/bulk_api.js:646:20)
    at DBCollection.insert (src/mongo/shell/collection.js:243:18)
    at (shell):1:12 at src/mongo/shell/collection.js:161

但是我可以使用Robomongo来存储它,但并非总是如此.显然我做错了.但是我不知道它是什么. 如果失败,那么我创建一个全新的收藏集,然后重试,它会成功.奇怪的东西超出了我的理解范围.

But I CAN STORE it using Robomongo, but not always. Obviously I am doing something wrong. But I have NO IDEA what it is. If it fails, and I create a brand new collection and try again, it succeeds. Weird stuff that goes beyond what I can comprehend.

但是当我尝试更新查询"中的值时,更改没有通过.绝不.有时甚至没有. 但是,我可以创建一个新对象并丢弃前一个对象.因此,解决方法就在那里.

But when I try updating values in the "query", changes are not going through. Never. Not even sometimes. I can however create a new object and discard the previous one. So, the workaround is there.

db.queries.update(
    {"name": "query1"},
    {"$set": { 
            ... update goes here ...
        }
    }
)

这样做将导致:

WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 52,
                "errmsg" : "The dollar ($) prefixed field '$or' in 'action.$or' is not valid for storage."
        }
})

似乎与上面的其他消息非常接近.

seems pretty close to the other message above.

需要说的是,我对这里发生的事情一无所知,所以我希望这里的一些巫师能够对此事有所了解

Needles to say, I am pretty clueless about what is going on here, so I hope some of the wizzards here are able to shed some light on the matter

推荐答案

我认为错误消息包含您需要考虑的重要信息:

I think the error message contains the important info you need to consider:

查询错误:字段名称不能以$

QUERY Error: field names cannot start with $

由于您要在文档中存储查询(或查询的一部分),因此最终将得到包含mongo运算符关键字(例如$or$ne$gt)的属性名. mongo文档实际上引用了这种确切的情况-强调已添加

Since you are trying to store a query (or part of one) in a document, you'll end up with attribute names that contain mongo operator keywords (such as $or, $ne, $gt). The mongo documentation actually references this exact scenario - emphasis added

字段名称不能包含点(即.)或空字符,并且它们不能以美元符号(即$)开头 ...


在这种情况下,我不会信任第三方应用程序,例如Robomongo.我建议直接在mongo shell中调试/测试此问题.


I wouldn't trust 3rd party applications such as Robomongo in these instances. I suggest debugging/testing this issue directly in the mongo shell.

我的建议是将查询的转义版本存储在您的文档中,以免干扰保留的运算符.您可以使用可用的JSON.stringify(my_obj);将部分查询编码为字符串,然后在以后选择检索它时对其进行解析/解码:JSON.parse(escaped_query_string_from_db)

My suggestion would be to store an escaped version of the query in your document as to not interfere with reserved operator keywords. You can use the available JSON.stringify(my_obj); to encode your partial query into a string and then parse/decode it when you choose to retrieve it later on: JSON.parse(escaped_query_string_from_db)

这篇关于在Mongo中存储查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆