如何从 mongodb 中获取值
how to grep the values from mongodb
开发新手。我正在尝试从 JSON 文件中获取值。有人可以帮我解决这个问题吗?
[{
"State": "New York",
"City": "Queens",
"Cars": {
"gas": {
"USAMade": {
"Ford": ["Fordcars", "Fordtrucks", "Fordsuv"]
},
"OutsideUS": {
"Toyota": ["Tcars", "Ttrucks", "TSUV"]
}
},
"electric": {
"USAMade": {
"Tesla": ["model3", "modelS", "modelX"]
},
"OutsideUS": {
"Nissan": ["Ncars", "Ntrucks", "NSUV"]
}
}
}
},
{
"State": "Atlanta",
"City": "Roswell",
"Cars": {
"gas": {
"USAMade": {
"Ford": ["Fordcars", "Fordtrucks", "Fordsuv"]
},
"OutsideUS": {
"Toyota": ["Tcars", "Ttrucks", "TSUV"]
}
},
"electric": {
"USAMade": {
"Tesla": ["model3", "modelS", "modelX"]
},
"OutsideUS": {
"Nissan": ["Ncars", "Ntrucks", "NSUV"]
}
}
}
}
]
- 如何列出汽车类型(gas/electric)?
- 一旦我得到类型,我想列出各自的制造国家(USAMade/OutsideUS)。
- 之后我想列出模型 (Ford/Toyota)?
假设您在文件 test.json 中有文档,这里是如何使用 linux shell 工具 cat,jq,sort,uniq:
1) cat test.json | jq '.[] | .Cars | keys[] ' | sort | uniq
"electric"
"gas"
2) cat test.json | jq '.[] | .Cars[] | keys[] ' | sort | uniq
"OutsideUS"
"USAMade"
3) cat test.json | jq '.[] | .Cars[][] | keys[] ' | sort | uniq
"Ford"
"Nissan"
"Tesla"
"Toyota"
如果您的数据在 mongoDB 中,我建议您将这个不同的值保存在单独集合中的单个文档中,并在从该集合加载时填充前端页面,文档看起来像这样:
{
State:["Atlanta","Oregon"],
City:["New York" , "Tokio" , "Moskow"],
Location:["OutsideUS" ,"USAMade"],
Model:["Ford","Toyota","Nissan"]
}
您不需要每次加载首页时都从数据库中提取不同的值,因为它不是可扩展的解决方案,并且在某些时候它会成为性能瓶颈...
但是,如果您仍然希望它根据选择仅从 mongoDB 中获取不同的键,您可以执行以下操作:
1.
mongos> db.test.aggregate([ {"$project":{"akv":{"$objectToArray":"$Cars"}}} ,{$unwind:"$akv"} ,{ $group:{_id:null , "allkeys":{$addToSet:"$akv.k"} } }] ).pretty()
{ "_id" : null, "allkeys" : [ "gas", "electric" ] }
mongos> db.test.aggregate([ {"$project":{"akv":{"$objectToArray":"$Cars.gas"}}} ,{$ unwind:"$akv"} ,{ $group:{_id:null , "allkeys":{$addToSet:"$akv.k"} } }]).pretty()
{ "_id" : null, "allkeys" : [ "USAMade", "OutsideUS" ] }
mongos> db.test.aggregate([ {"$project":{"akv":{"$objectToArray":"$Cars.gas.USAMade"}}} ,{$unwind:"$akv"} ,{ $group:{_id:null , "allkeys":{$addToSet:"$akv.k"} } }] ).pretty()
{ "_id" : null, "allkeys" : [ "Ford" ] }
开发新手。我正在尝试从 JSON 文件中获取值。有人可以帮我解决这个问题吗?
[{
"State": "New York",
"City": "Queens",
"Cars": {
"gas": {
"USAMade": {
"Ford": ["Fordcars", "Fordtrucks", "Fordsuv"]
},
"OutsideUS": {
"Toyota": ["Tcars", "Ttrucks", "TSUV"]
}
},
"electric": {
"USAMade": {
"Tesla": ["model3", "modelS", "modelX"]
},
"OutsideUS": {
"Nissan": ["Ncars", "Ntrucks", "NSUV"]
}
}
}
},
{
"State": "Atlanta",
"City": "Roswell",
"Cars": {
"gas": {
"USAMade": {
"Ford": ["Fordcars", "Fordtrucks", "Fordsuv"]
},
"OutsideUS": {
"Toyota": ["Tcars", "Ttrucks", "TSUV"]
}
},
"electric": {
"USAMade": {
"Tesla": ["model3", "modelS", "modelX"]
},
"OutsideUS": {
"Nissan": ["Ncars", "Ntrucks", "NSUV"]
}
}
}
}
]
- 如何列出汽车类型(gas/electric)?
- 一旦我得到类型,我想列出各自的制造国家(USAMade/OutsideUS)。
- 之后我想列出模型 (Ford/Toyota)?
假设您在文件 test.json 中有文档,这里是如何使用 linux shell 工具 cat,jq,sort,uniq:
1) cat test.json | jq '.[] | .Cars | keys[] ' | sort | uniq
"electric"
"gas"
2) cat test.json | jq '.[] | .Cars[] | keys[] ' | sort | uniq
"OutsideUS"
"USAMade"
3) cat test.json | jq '.[] | .Cars[][] | keys[] ' | sort | uniq
"Ford"
"Nissan"
"Tesla"
"Toyota"
如果您的数据在 mongoDB 中,我建议您将这个不同的值保存在单独集合中的单个文档中,并在从该集合加载时填充前端页面,文档看起来像这样:
{
State:["Atlanta","Oregon"],
City:["New York" , "Tokio" , "Moskow"],
Location:["OutsideUS" ,"USAMade"],
Model:["Ford","Toyota","Nissan"]
}
您不需要每次加载首页时都从数据库中提取不同的值,因为它不是可扩展的解决方案,并且在某些时候它会成为性能瓶颈...
但是,如果您仍然希望它根据选择仅从 mongoDB 中获取不同的键,您可以执行以下操作:
1.
mongos> db.test.aggregate([ {"$project":{"akv":{"$objectToArray":"$Cars"}}} ,{$unwind:"$akv"} ,{ $group:{_id:null , "allkeys":{$addToSet:"$akv.k"} } }] ).pretty()
{ "_id" : null, "allkeys" : [ "gas", "electric" ] }
mongos> db.test.aggregate([ {"$project":{"akv":{"$objectToArray":"$Cars.gas"}}} ,{$ unwind:"$akv"} ,{ $group:{_id:null , "allkeys":{$addToSet:"$akv.k"} } }]).pretty() { "_id" : null, "allkeys" : [ "USAMade", "OutsideUS" ] }
mongos> db.test.aggregate([ {"$project":{"akv":{"$objectToArray":"$Cars.gas.USAMade"}}} ,{$unwind:"$akv"} ,{ $group:{_id:null , "allkeys":{$addToSet:"$akv.k"} } }] ).pretty()
{ "_id" : null, "allkeys" : [ "Ford" ] }