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

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

问题描述

I have the following document:

{
    _id     : 1,

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

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

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

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"}).

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-?

解决方案

I'd recommend restructuring the model.

{
    _id     : 1,

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

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

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

And the query:

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

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天全站免登陆