在mongoDB中有序的固定长度数组

时间:2021-04-18 21:42:14

I'm new to monogDB and trying to design the way I store my data so I can do the kinds of queries I want to. Say I have a document that looks like

我是monogDB的新手,并尝试设计我存储数据的方式,以便我可以进行我想要的各种查询。说我有一个看起来像的文件

{
    "foo":["foo1","foo2","foo3"],
    "bar":"baz"
}

Where the array "foo" is always of length 3, and the order of the items are meaningful. I would like to be able to make a query that searches for all documents where "foo2" == something. Essentially I want to treat "foo" like any old array and be able to index it in a search, so something like "foo"[1] == something.

数组“foo”总是长度为3,并且项目的顺序是有意义的。我希望能够查询搜索“foo2”==某些内容的所有文档。基本上我想像任何旧数组一样对待“foo”并且能够在搜索中对其进行索引,所以像“foo”[1] ==某些东西。

Does monogDB support this? Would it be more correct to store my data like,

monogDB支持这个吗?存储我的数据是否更正确,

{
    "foo":{
        "foo1":"val1",
        "foo2":"val2",
        "foo3":"val3"
    },
    "bar":"baz"
}

instead? Thanks.

代替?谢谢。

1 个解决方案

#1


2  

The schema you have asked about is fine.

您询问的架构很好。

To insert at a specific index of array: Use the $position operator. Read here.

要插入数组的特定索引:使用$ position运算符。在这里阅读

To query at a specific index location: Use the syntax key.index. As in:

要在特定索引位置进行查询:使用语法key.index。如:

db.users.find({"foo.1":"foo2"})

#1


2  

The schema you have asked about is fine.

您询问的架构很好。

To insert at a specific index of array: Use the $position operator. Read here.

要插入数组的特定索引:使用$ position运算符。在这里阅读

To query at a specific index location: Use the syntax key.index. As in:

要在特定索引位置进行查询:使用语法key.index。如:

db.users.find({"foo.1":"foo2"})