首页 \ 问答 \ MongoDB - 从对象内的数组中查找数据(MongoDB - Find Data from a array inside a object)

MongoDB - 从对象内的数组中查找数据(MongoDB - Find Data from a array inside a object)

我有以下形式的数据库

{
"_id" : ObjectId("56fc92adcf908c9f296e5885"),
"_class" : "com.abc.ppm.dto.AccessAttemptDTO",
"url" : "/myProject/edit",
"ipAddress" : "127.0.0.1",
"param" : {
    "iecode" : [ 
        "P1234"
    ],
    "id" : [ 
        "5696578c6d34f835dc632fdd "
    ]
},
"accessTime" : "30-3-2016 10:59:57",
"email" : "abc@gmail.com",
"entity" : "admin",
"entityCode" : ""
},

{
"_id" : ObjectId("56fc92adcf908c9f296e5887"),
"_class" : "com.abc.ppm.dto.AccessAttemptDTO",
"url" : "/myProject/edit",
"ipAddress" : "127.0.0.1",
"param" : {
    "iecode" : [ 
        "P1122"
    ],
    "id" : [ 
        "5696578c6d34f835dc632fdd "
    ]
},
"accessTime" : "30-3-2016 10:59:57",
"email" : "abc@gmail.com",
"entity" : "admin",
"entityCode" : ""

}

现在我想找到iecode为'P1234'的所有enries。 我怎么做? (注意:iecodes值以字符串数组格式存储。)


I have my db in following form

{
"_id" : ObjectId("56fc92adcf908c9f296e5885"),
"_class" : "com.abc.ppm.dto.AccessAttemptDTO",
"url" : "/myProject/edit",
"ipAddress" : "127.0.0.1",
"param" : {
    "iecode" : [ 
        "P1234"
    ],
    "id" : [ 
        "5696578c6d34f835dc632fdd "
    ]
},
"accessTime" : "30-3-2016 10:59:57",
"email" : "abc@gmail.com",
"entity" : "admin",
"entityCode" : ""
},

{
"_id" : ObjectId("56fc92adcf908c9f296e5887"),
"_class" : "com.abc.ppm.dto.AccessAttemptDTO",
"url" : "/myProject/edit",
"ipAddress" : "127.0.0.1",
"param" : {
    "iecode" : [ 
        "P1122"
    ],
    "id" : [ 
        "5696578c6d34f835dc632fdd "
    ]
},
"accessTime" : "30-3-2016 10:59:57",
"email" : "abc@gmail.com",
"entity" : "admin",
"entityCode" : ""

}

Now I want to find all the enries where iecode is 'P1234'. How do I do that? (Note: iecodes value is stored in string array format.)


原文:https://stackoverflow.com/questions/37087048
更新时间:2022-09-06 13:09

最满意答案

解决方案很简单,但IDE和调试器都没有说明它。 它只返回null 。 当您使用php artisan config:cache ,根据文档:

如果您在部署过程中执行php artisan config:cache命令,则应确保只从配置文件中调用env()函数。

显然我在配置文件之外有env变量,所以在缓存之后我再也无法在外面使用它了。 php artisan config:clear让它重新投入使用。

我发现更多关于env的用法,它只能在配置文件中使用。 您可以使用其他帮助程序方法config()从项目的其余部分访问env变量。 确保将其分配给配置文件中的另一个键,例如'key' => env('CACHE_DRIVER')

更重要的是,你必须记住每次你改变.env文件时运行php artisan config:cache 。 Laravel不会加载新值,直到它被缓存。 如果它没有被缓存,则不需要这样做。


The solution is simple, but not IDE nor debugger tells anything about it. It just returns null. When you use php artisan config:cache, according to documentation:

If you execute php artisan config:cachecommand during your deployment process, you should be sure that you are only calling the env() function from within your configuration files.

Obviously I have env variables outside the config files, so after caching I was not able to use it outside anymore. The php artisan config:clear puts it back to work.

What I've found more about the usage of env, that it should be used only within config files. You can access env variables from the rest of the project using other helper method config(). Be sure to assign it to another key in config file, e.g. 'key' => env('CACHE_DRIVER')

What is more, you have to remember to run php artisan config:cache each time you will change .env file. Laravel will not load the new values, until it's cached. If it's not cached, no need to do it.

相关问答

更多
  • 从官方Laravel 5.2升级说明: 如果您在部署期间使用config:cache命令,则必须确保您只在配置文件中调用env函数,而不是在应用程序中的任何其他位置调用该函数。 如果您在应用程序中调用env ,强烈建议您将适当的配置值添加到配置文件中,然后从该位置调用env ,以允许将env调用转换为config调用。 参考: https : //laravel.com/docs/5.2/upgrade#upgrade-5.2.0 Wow. Good grief. It's because I had an ...
  • 解决方案很简单,但IDE和调试器都没有说明它。 它只返回null 。 当您使用php artisan config:cache ,根据文档: 如果您在部署过程中执行php artisan config:cache命令,则应确保只从配置文件中调用env()函数。 显然我在配置文件之外有env变量,所以在缓存之后我再也无法在外面使用它了。 php artisan config:clear让它重新投入使用。 我发现更多关于env的用法,它只能在配置文件中使用。 您可以使用其他帮助程序方法config()从项目的其 ...
  • 当然,我自己也倾向于每次都“弯曲”一个框架,总有一种方法,但并不总是最好的解决方案。 我并没有在这里给出一个完整的实现,只是指出你的方向,这可能对你有用。 您可以扩展Laravel的基本应用程序类Illuminate\Foundation\Application ,其中包含在应用程序引导期间加载的$environmentFile变量存储环境文件,或者可能覆盖函数loadEnvironmentFrom($file)或environmentFile() 。 整个逻辑取决于你。 所以基本上你需要做的只是为了能够使 ...
  • 启动Dotenv以在TestCase阶段获得.env变量 public function createApplication() { $app = require __DIR__.'/../bootstrap/app.php'; $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); Dotenv::load(__DIR__.'/../'); $this->baseUrl = env('APP_URL' ...
  • 我一直认为.env文件只是设置服务器的一部分。 git会忽略.env文件,所以一旦设置完毕,你可以推送/拉动你内心的内容,它会单独保留.env文件。 到目前为止,至少我是如何做到的。 I always thought that the .env file was just part of setting up your server. The .env file is ignored by git, so once it's setup, you can push/pull to your heart's ...
  • 您可以将APP_ENV环境变量转储到托管JavaScript的页面,稍后访问它或将其传递给Vue。 然后在你的控制器中... $env = getenv("APP_ENV"); 会让你获得APP_ENV的价值 You could dump the APP_ENV environment variable to the page hosting your JavaScr ...
  • 宝石提供了一个发电机: $ rails generate figaro:install 生成器创建一个config / application.yml文件并修改.gitignore文件以防止将文件检入到git存储库中。 您可以将环境变量作为键/值对添加到config / application.yml中 : GMAIL_USERNAME: Your_Username 环境变量将作为ENV变量在您的应用程序中的任何位置可用: ENV["GMAIL_USERNAME"] 这为您提供了在代码中使用相同变量的 ...
  • Laravel使用配置文件从.env文件中获取数据。 因此,您可以做的是在运行时覆盖配置值 : config(['database.default' => 'mysql']); Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime: config(['database.default' => 'mysql']); ...
  • 每个单独的命令(前缀为- )都在自己的shell中运行。 这就是您获取的环境变量在以下命令中不存在的原因。 我看到有三种方式: 1)在circle.yml定义环境变量。 我知道你说你不想这样做,但这是迄今为止最简单,最明确的方法。 2)您可以使用source命令为需要变量的行添加前缀。 例如: test: override: - source ./config/test_config.sh; nvm use 7.9 && npm test 3)利用多线YAML: test: overrid ...
  • 您无法从angular读取.env文件,因为此文件不公开,客户端显然无法访问。 要公开一些后端配置,您有一些替代方案: 暴露配置api,在应用程序准备就绪时通过$ http调用它(使用.run())并将检索到的数据保存在服务中,以便您可以在前端组件之间共享配置 创建一个后端服务,该服务呈现为js文件,并将此js文件包含为标准角度组件 在ng-app节点上使用服务器定义的ng-init,以便通过角度范围访问配置 在我看来,第一个是最好的。 You cannot read .env files from ang ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)