Object.keys,如何在mongodb中获取键列表 [英] Object.keys, how to get a list of keys in mongodb

查看:301
本文介绍了Object.keys,如何在mongodb中获取键列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
"_id": "1",
"style": "13123",
"category": "dress",
"colors": {
    "Black": {
        "prestock": 50,
        "instock": 60,
        "inactive": 0
        },
    "Blue": {
        "prestock": 30,
        "instock": 0,
        "inactive": 0
        },
    "Red": {
        "prestock": 10,
        "instock": 60,
        "inactive": 0
        }
  }
}

我在json以上,我需要访问预备库存,库存和非活动状态. 但是颜色的值会根据样式而改变. 例如:

i have above json, i need to access to prestock, instock, and inactive. but colors' value will change depending on styles. for example:

{
 "_id": "2",
 "style": "14321", 
 "category": "top",
 "colors": {
     "Green": {
        "prestock": 50,
        "instock": 60,
        "inactive": 0
         }, 
     "Yellow": {
        "prestock": 50,
        "instock": 60,
        "inactive": 0
         }
      }
}

如何在mongodb中查询此内容? 这与Object.keys(obj)有关吗?

how can i query this in mongodb? would this something that's related to Object.keys(obj)?

ps.如果这是一个重复的问题,请指导我!

ps.if this is a duplicate question please guide me!

谢谢!

推荐答案

根据{"colors.*.prestock" : {$gte:30}}的查询rel ="nofollow"> SERVER-267 ,我怀疑这在未来几年内将得到支持.

A query like {"colors.*.prestock" : {$gte:30}} isn't possible according to SERVER-267, and I doubt this will be supported in the next years.

您最好的选择是将架构更改为数组:

Your best bet is to change the schema to an array:

colors: [
 { "color" : "Green", "instock" : 50, ... },
 { "color" : "Yellow", "instock" : 50, ... },
]

然后您可以查询

db.foo.find( {"colors.prestock" : {$gte:30}} )

请注意,这将返回整个对象,包括所有颜色,即查询约束不成立的所有颜色.可以使用聚合框架解决此问题,但同样,只能使用$unwind,这也要求colors是数组.

Note that this will return the entire object, including all colors, i.e. also those for which the query constraint doesn't hold. This could be solved using the aggregation framework, but again, only using $unwind which also requires colors to be an array.

这篇关于Object.keys,如何在mongodb中获取键列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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