在 mongodb 中存储任意 key:value 数组的更好方法是什么

发布于 2024-09-30 04:01:25 字数 325 浏览 0 评论 0原文

我有一个键值数组

{val1: [{性别:男},{年龄:23​​},{最喜欢的颜色:红色}] 关于这个数组

的问题是,它绝不是结构化的,而且我也不能保证再次看到相同的键值。例如,我可以看到 val2

{val2: [{性别:男},{头发颜色:红色},{身高:6'1"}] ?

我的问题是,将这些键中的每一个作为 mongo 中的一列更好,还是应该将它们保留在数组中 我知道使用 mongodb 可以创建任意数量的列,因此理论上任何一种方法都可以工作。如果我想查询一个数组是否包含“性别”键和“男性”值,那么在我将值保留在对象的嵌套数组中的示例中是否可能?

I have an array of key values

{val1:
[{gender:male}, {age:23}, {favorite-color:red}]
}

The thing about this array is that in no way is it structured and in no way am I ever guaranteed to see the same key value again. For example I could see the following for val2

{val2:
[{sex:male}, {hair-color:red}, {height:6'1"}]
}

My question is whether is is better to have each of these keys as a column in mongo, or should I keep them in an array? I know with mongodb it is possible to create an arbitrarily large number of columns so either way would theoretically work. If I wanted to query if an array contained the key of 'sex' with a value of 'male', would that be possible in the example where I keep the values in a nested array in the object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的鱼塘能养鲲 2024-10-07 04:01:25

valX 到底有什么用?我只会使用常规的“列”。

john : { gender:"male", age:23, favorite-color:red }
bob : { gender:"male", age:25, favorite-color:blue }
susan : { gender:"female", age:20, favorite-color:yellow }

您可以使用基本查询针对这 3 个字段进行搜索。

What's valX for anyways? I would just use regular "columns."

john : { gender:"male", age:23, favorite-color:red }
bob : { gender:"male", age:25, favorite-color:blue }
susan : { gender:"female", age:20, favorite-color:yellow }

You can search for against the 3 fields using basic queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文