回复头部信息并将提取数组添加到相同的结果 [英] Reply head informations and add extract array to same result
本文介绍了回复头部信息并将提取数组添加到相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我解决这种情况吗?
我有这个假的 JSON...
<预><代码>[{用户":{类型":PF",代码":12345,名称":达斯维达",货币":BRL",状态":单身",本地化":NABOO",创建日期":1627990848665,旧地址":[{定位":死亡之星",创建日期":1627990848775},{本地化":塔图因",创建日期":1627990555888},]}}]我的想法是,我需要提取olderAdress"并创建新的寄存器,但我也需要保留原始寄存器.示例:这是我希望的结果.
<预><代码>[{_id":ObjectId(5a3456e000102030405000000"),用户":{名称":达斯维达",代码":12345,创建日期":1627990848665,货币":BRL",本地化":NABOO",状态":单身",类型":PF"}},{_id":ObjectId(5a789e000102030405000000"),用户":{名称":达斯维达",代码":12345,创建日期":1627990848775,货币":BRL",定位":死亡之星",状态":单身",类型":PF"}},{_id":ObjectId(5a991e000102030405000000"),用户":{名称":达斯维达",代码":12345,创建日期":1627990555888,货币":BRL",本地化":塔图因",状态":单身",类型":PF"}}]我在这个链接(测试提取值)中尝试了同样的想法,但不幸的是我不能.有人可以帮我吗?
解决方案
你已经很接近了,你要做的只是在 $unwind
之前将新位置添加到数组中.
像这样:
db.collection.aggregate([{$addFields":{user.olderAdress":{$concatArrays":[$user.olderAdress",[{本地化":NABOO",创建日期":$$NOW"}]]}}},... 管道的其余部分 ...])
someone colud please help me with this situation?
I have this fake JSON...
[
{
"user": {
"type": "PF",
"code": 12345,
"Name": "Darth Vader",
"currency": "BRL",
"status": "SINGLE",
"localization": "NABOO",
"createDate": 1627990848665,
"olderAdress": [
{
"localization": "DEATH STAR",
"createDate": 1627990848775
},
{
"localization": "TATOOINE",
"createDate": 1627990555888
},
]
}
}
]
My idea is, i need to extract the "olderAdress" and create new register but I need to keep the original register too. Example: This is the result I hope.
[
{
"_id": ObjectId("5a3456e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate":1627990848665,
"currency": "BRL",
"localization": "NABOO",
"status": "SINGLE",
"type": "PF"
}
},
{
"_id": ObjectId("5a789e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate": 1627990848775,
"currency": "BRL",
"localization": "DEATH STAR",
"status": "SINGLE",
"type": "PF"
}
},
{
"_id": ObjectId("5a991e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate": 1627990555888,
"currency": "BRL",
"localization": "TATOOINE",
"status": "SINGLE",
"type": "PF"
}
}
]
I try same think in this link (Test to extract values) to tests but unfortunately I cant. Somewoone could please help me?
解决方案
You're very close, what you want to do is just to add the new location to the array before $unwind
ing it.
like so:
db.collection.aggregate([
{
"$addFields": {
"user.olderAdress": {
"$concatArrays": [
"$user.olderAdress",
[
{
"localization": "NABOO",
"createDate": "$$NOW"
}
]
]
}
}
},
... rest of pipeline ...
])
这篇关于回复头部信息并将提取数组添加到相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文