[英]node api to fetch subdocuments using mongoose
My MongoDB stores documents having the following structure:我的 MongoDB 存储具有以下结构的文档:
{
"application_detail":{},
"curl_detail":{
"Curl1":{
"key1":"value1",
"key2":"value2"
},
"Curl2":{
"key1":"value1",
"key2":"value2"
},
"Curl3":{
"key1":"value1",
"key2":"value2"
},
"Curl4":{
"key1":"value1",
"key2":"value2"
},
/*total number of curls are unknown*/
}
}
How can I fetch key1
for all the curls present under curl_detail
from mongoDB using express and mongoose?如何使用 express 和
curl_detail
从 mongoDB 中获取 curl_detail 下存在的所有卷发的key1
?
Expected Output:预期输出:
{
"curl_detail": {
"Curl1": {
"key1": "value1"
},
"Curl2": {
"key1": "value1"
},
"Curl3": {
"key1": "value1"
},
"Curl4": {
"key1": "value1"
}
}
}
NOTE: Total number of curls are unknown.注意:卷发总数未知。
I would like to propose you changing data structrure from我想建议你改变数据结构
"curl_detail": {
"Curl1": {
"key1": "value1",
"key2": "value2"
},
"Curl2": {
"key1": "value1",
"key2": "value2"
}
}
to到
"curl_detail": [{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value1",
"key2": "value2"
}
]
Storing data structure in array will allow you to find some data in an easier way.将数据结构存储在数组中可以让您以更简单的方式找到一些数据。 Also there's no need to have object with properties like
Curl1
, Curl2
etc.此外,不需要具有
Curl1
、 Curl2
等属性的对象。
For the new data structure there are already some answers with the explanation:对于新的数据结构,已经有一些答案和解释:
1) Mongo find value with unknown parent key 1) Mongo 查找父键未知的值
2) Query MongoDB by value when parent key is unknown 2)当父键未知时按值查询MongoDB
3) MongoDB: Find document given field values in an object with an unknown key 3) MongoDB:在具有未知键的对象中查找给定字段值的文档
UPDATE更新
If there's no possibility to change the data structure please see this solution (trick with use of $arrayToObject
/ $objectToArray
to determine the name of nested key eg "Curl1"):如果无法更改数据结构,请参阅此解决方案(使用
$arrayToObject
/ $objectToArray
$arrayToObject
来确定嵌套键的名称,例如“Curl1”的技巧):
db.collection.aggregate([{
$addFields: {
curl_detail: {
$arrayToObject: {
$map: {
input: {
$objectToArray: "$curl_detail"
},
as: "details",
in: {
k: "$$details.k",
v: {
key1: "$$details.v.key1"
}
}
}
}
}
}
},
{
$project: {
_id: 0,
curl_detail: 1
}
}
])
This outputs the following:这将输出以下内容:
[
{
"curl_detail": {
"Curl1": {
"key1": "value1"
},
"Curl2": {
"key1": "value1"
},
"Curl3": {
"key1": "value1"
},
"Curl4": {
"key1": "value1"
}
}
}
]
You can check it through Mongo Playground您可以通过Mongo Playground查看
You can use projection for the query you are looking for:您可以对要查找的查询使用投影:
collection.find({}, {"curl_detail":1}, (findError, results) => {
if(findError) {
//handle the error
return;
}
// here is the expected result
console.log();
});
If this result also works for you:如果此结果也适用于您:
[
{
"curl_detail": {
"Curl1": "value1",
"Curl2": "value1",
"Curl3": "value1",
"Curl4": "value1"
}
}
]
You can use the following aggregation using the $objectToArray and $arrayToObject您可以使用使用下面的聚集$ objectToArray和$ arrayToObject
db.collection.aggregate([
{
$addFields: {
curl_detail: {
$map: {
"input": {
"$objectToArray": "$curl_detail"
},
"as": "el",
"in": {
"k": "$$el.k",
"v": "$$el.v.key1",
}
}
}
}
},
{
$project: {
_id: 0,
curl_detail: {
$arrayToObject: "$curl_detail"
}
}
}
])
You can use this aggregation with mongoose like this, let's say your model is MyModel:您可以像这样将这种聚合与猫鼬一起使用,假设您的模型是 MyModel:
MyModel.aggregate([
{
$addFields: {
curl_detail: {
$map: {
input: {
$objectToArray: '$curl_detail'
},
as: 'el',
in: {
k: '$$el.k',
v: '$$el.v.key1'
}
}
}
}
},
{
$project: {
_id: 0,
curl_detail: {
$arrayToObject: '$curl_detail'
}
}
}
]).then(res => {
console.log(res);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.