首页 \ 问答 \ 如何用一个正则表达式(或类似于mongo)来最好地检索文档的特定子元素,(How to best retrieve specific sub-elements of a document with a regex (or similar in mongo))

如何用一个正则表达式(或类似于mongo)来最好地检索文档的特定子元素,(How to best retrieve specific sub-elements of a document with a regex (or similar in mongo))

我有一个具有以下结构的文档,是否有可能(例如使用$ regex)仅返回开始“^ raw_”的位置元素(因为可能有多种raw_元素变体)?

我可以很容易地用一个简单的投影返回所有的“职位”元素,但似乎无法弄清楚(看文档中的例子)如果/我如何才能拔出那些以raw_开头的元素(我尝试了几个变体$ elemMatch但没有多少运气)。

以某种方式可能吗? 有人可以提供一个例子吗?

{ "_id": '1', "positions": { "raw_1": { "123": 125.30, "456": 125.30, "789": 125.30, }, "raw_2": { "123": 125.30, "456": 125.30, "789": 125.30, }, "calc_1": { "123": 125.30, "456": 125.30, "789": 125.30, }, "calc_2": { "123": 125.30, "456": 125.30, "789": 125.30, } } }


I have a document with the following structure, is it possible (e.g. using $regex) to return just the position elements which start "^raw_" (as there can be many variants of raw_ elements)?

I can easily return all "positions" elements with a simple projection but can't seem to figure out (looking at examples in the doc's) if/how I can pull out just those that start with raw_ (I have tried a few variations of $elemMatch but without much luck).

Is it possible somehow? Can someone provide an example if so?

{ "_id": '1', "positions": { "raw_1": { "123": 125.30, "456": 125.30, "789": 125.30, }, "raw_2": { "123": 125.30, "456": 125.30, "789": 125.30, }, "calc_1": { "123": 125.30, "456": 125.30, "789": 125.30, }, "calc_2": { "123": 125.30, "456": 125.30, "789": 125.30, } } }


原文:https://stackoverflow.com/questions/46503534
更新时间:2021-06-07 09:06

最满意答案

我不认为有可能使用$ regex来查询文档键。 如果可以调整文档结构,我建议如下:

{
    "_id": "1",
    "positions": {
        "raw": [
            {
                "entry": "1",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            },
            {
                "entry": "2",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            }
        ],
        "calc": [
            {
                "entry": "1",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            },
            {
                "entry": "2",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            }
        ]
    }
}

鉴于此结构,仅返回您感兴趣的原始条目要容易得多:

db.col1.find({ "_id" : "1" },{ "positions.raw" : true });

I don't think that it' possible to query against the document keys with $regex. If it's possible to adjust the document structure, I suggest the following:

{
    "_id": "1",
    "positions": {
        "raw": [
            {
                "entry": "1",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            },
            {
                "entry": "2",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            }
        ],
        "calc": [
            {
                "entry": "1",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            },
            {
                "entry": "2",
                "values": {
                    "123": 125.30,
                    "456": 125.30,
                    "789": 125.30,
                 }
            }
        ]
    }
}

Given this structure, it is much easier to return only the raw entries you are interested in:

db.col1.find({ "_id" : "1" },{ "positions.raw" : true });

相关问答

更多

如何用一个正则表达式(或类似于mongo)来最好地检索文档的特定子元素,(How to best retrieve specific sub-elements of a document with a regex (or similar in mongo))

我不认为有可能使用$ regex来查询文档键。 如果可以调整文档结构,我建议如下: { "_id": "1", "positions": { "raw": [ { "entry": "1", "values": { "123": 125.30, "456": 125.30, ...

通过mongo中的所有集合迭代正则表达式(Iterate regex through all collections in mongo)

TheCoo变量是Cursor对象。 您可以通过调用光标上的toArray方法获取所有文档的数组: TheCoo.toArray(function (err, documents) { printjson(documents); }); The TheCoo variable is a Cursor object. You can obtain an array of all the documents by calling the toArray method on the cursor: ...

正则表达式隔离特定子字符串(Regex to isolate a specific substring)

这应该适合您的需要: \d+(.*) 这将搜索一个或多个数字( \d+ ),然后将所有内容放入一个组中: . = any character * = repeater (zero or more of the preceding value (which is any character in the above () = grouping 但是,杰森是正确的,因为你只需要使用分割功能 This should work for what you need: \d+(.*) This sear ...

Ruby检索最后Upserted Mongo文档(Ruby Retrieve the Last Upserted Mongo Document)

Ruby MongoDB驱动程序需要单独的数据库调用来检索数据。 虽然它可以执行upsert,但是没有办法通过单个数据库调用来获取upserted文档。 所以你必须做这样的事情 coll.find({ :count => "foobar" }) The Ruby MongoDB Driver requires a separate database call to retrieve data. Although it can perform an upsert there is no way t ...

Mongo正则表达式查询删除引号(Mongo regex query remove quotes)

这是我发现保留引号的方式: db.getCollection('products').find({ "$and": [ { "$or": [ { "category": { "$regex": ".*cocinar.*", "$options": "i" } }, { "category": { ...

在Mongo中检索子文档(Retrieve Sub Document in Mongo)

也许这会有所帮助: db.myCollection.find({"id":"1","data.id":"2"},{"id":1,"data.$":1}) 它不需要传递$和 Maybe this will help: db.myCollection.find({"id":"1","data.id":"2"},{"id":1,"data.$":1}) Its not required to pass $and

使用mongo-delphi-driver获取具有特定_id的文档(Get a document with a specific _id with mongo-delphi-driver)

正如它在这里所说,严格模式JSON接口仅用于HTTP API和命令行工具。 驱动程序应使用适当的BSON。 我想你可以在Delphi中这样做: b := mongo.findOne(ns(BSON(['_id',TBsonOID.Create('58c6b6af9b2dcd04ae46844d')]))); 但我不是真正的德尔福人。 编辑:您添加的TBsonOID示例应该是这样我认为: var bb : TBsonBuffer; query, b : TBson; id : TBso ...

如何使用mongo cxx驱动程序检索特定字段的值(How to retrieve the value of a specific field using mongo cxx driver)

这是一个例子: #include <iostream> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/options/find.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/uri.hpp> using bsoncxx::b ...

相关文章

更多

最新问答

更多
  • 如何从远程文件拉取文件而不覆盖本地文件?(How do I pull files from remote without overwriting local files?)
  • Reactjs:状态改变时重新渲染iframe(Reactjs: re-renders iframes when state changed)
  • 奇怪的网址,以及跟随php页面流程的困难(odd url, and difficulty in following the php page flow)
  • 标签活动无效(Tab Activity is not working)
  • JavaME合适的语法编译器建议?(JavaME-suitable grammar compiler recommendations?)
  • 指定参数(Specifying arguments)
  • 可以通过Ruby插件或控制台覆盖Sketchup中的键盘快捷键吗?(Can one override keyboard shortcuts in Sketchup through the a Ruby Plugin or Console?)
  • 计算Java EE Web App中用户数的最佳方法(Best way to count number of users in a Java EE web App)
  • 无法使用templateUrl加载cordova中的外部模板(unable to load external templates in cordova with templateUrl)
  • PHPExcel:写入期间无法使用缓存(PHPExcel: Unable to use cache during write)
  • 在javascript中嵌套这个指针(nested this pointer in javascript)
  • 谁跟领航致远培训过,有问题问下啊
  • 控制器要求在入门时下载(Controller ask to download on entry)
  • 未能通过conda安装Asyncio(Failure to install Asyncio via conda)
  • 如何查找已完成项目的总长度?(How to find length of total completed items?)
  • 如何检查OleInitialize是否已被调用?(How to check if OleInitialize has already been called?)
  • SQL在特定范围内返回列中具有最大值的行(SQL Returning rows with max value in column, within a specific range)
  • preg_match从url获取id(preg_match get the id from url)
  • 如何在运算符中为make方程转换perl变量?(How to convert a perl variable in a operator for make equations?)
  • 在导航上方添加空格/标题。(Add a white space/ header above navigation.)
  • MeetingItem已保存;(MeetingItem saved; but change now shown in Calendar)
  • c#vb:我们应该使用System.Lazy进行资源密集型任务吗?(c# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed))
  • 为什么在armeabi代码中使用armeabi-v7a代码?(Why use armeabi-v7a code over armeabi code?)
  • 获取请求的自定义标头(Java HTTP)(Fetching a custom header of a request (Java HTTP))
  • 是否可以在嵌套的if语句中从varchar转换为numeric以动态评估参数?(Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?)
  • 如何将Html.ActionLink转换为链接到Ajax调用的按钮?(How to convert from Html.ActionLink to a button linked to Ajax call?)
  • 应用程序如何处理Windows符号链接?(How are Windows symbolic links treated by the apps?)
  • html,js,css在jsfiddle中工作,但不在sharepoint中(html, js, css works in jsfiddle but not in sharepoint)
  • 从Ruby脚本调用Elasticsearch Rest API(Calling Elasticsearch Rest API from Ruby script)
  • 如何将嵌套setTimeouts转换为承诺(How to convert nested setTimeouts to promises)