如何查询动态键 - mongodb schema design [英] How to query a dynamic key - mongodb schema design

查看:16
本文介绍了如何查询动态键 - mongodb schema design的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

{
    _id     : 1,

    key1    : {
                  samekeyA : "value1",
                  samekeyB : "value2"
              },

    key2 :    {
                  samekeyA : "value3",
                  samekeyB : "value4"
              },

    key3 :    {
                  samekeyA : "value5",
                  samekeyB : "value6"
              }  
}

以上;key1, key2key3 用于证明我不知道完整的密钥,除了它的前缀;但是我知道的内键 samekeyAsamekeyB .我需要这样的查询:db.coll.find({"key*.samekeyA":"value1"}).

Above; key1, key2 and key3 are given to demonstrate that I don't know the full key, except a prefix of it; but inner keys samekeyA and samekeyB those I know. I require queries such: db.coll.find({"key*.samekeyA":"value1"}).

我认为没有一种 mongo 方式 - 正则表达式键查询? - 来实现这一点,所以有什么想法吗?我应该改造我的文档 -tree-?

I think there isn't a mongo way - regex key queries?- to accomplish that, so any ideas on that? Should I remodel my document -tree-?

推荐答案

我建议重构模型.

{
    _id     : 1,

    data: [   {
                  key      : "key1",
                  samekeyA : "value1",
                  samekeyB : "value2"
              },

              {
                  key      : "key2",
                  samekeyA : "value3",
                  samekeyB : "value4"
              },

              {
                  key      : "key3",
                  samekeyA : "value5",
                  samekeyB : "value6"
              }  
    ]
}

和查询:

db.col.find({"data.samekeyA": "value1"})

目前(也可能在未来)无法在字段名中使用通配符查询 MongoDB 集合(感谢 @gWiz).

At the moment (and probably in the future, too) it's not possible to query MongoDB collections with wildcards in fieldnames (thanks to @gWiz).

这篇关于如何查询动态键 - mongodb schema design的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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