如何查询动态密钥-MongoDB模式设计 [英] How to query a dynamic key - mongodb schema design

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

问题描述

我有以下文件:

{
    _id     : 1,

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

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

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

以上;给出key1key2key3的目的是证明我不知道全键,只是它的前缀.但是我知道的内键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模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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