mongoose - 合并猫鼬中的字段
问题描述
我现在的猫鼬是
var selectDetail =person.find({_id:'the id of the person'})
.select('person.first_name person.last_name')
.exec(function(err, result){
if(!err){
console.log(result)
}
})
生产
[{ person: { first_name: 'Samuel', last_name: 'Sharon'}, _id: 5b84c718bcc9d9114c34bf0a },
{ person: { first_name: 'Jesse', last_name: 'James'}, _id: 5b86fcdf583643014489261b },
{ person: { first_name: 'Solomon', last_name: 'Saunders'}, _id: 5b86fcdf583643014489261b },
{ person: { first_name: 'Blake', last_name: 'hill'}, _id: 5b86fcdf583643014489261b }]
生产
[{79ahjhhajhga986786a78ajg, Samuel Sharon},
{5sghjkhs798s798sg798s98g, Jesse James},
{56fghjajhga678hj6877866v, Solomon Saunders},
{2364jhadhkjdaf76ajhdfa76, Blake Hill}]
我正在认真寻找出路 我对我之前写的内容进行了一些修改
解决方案
由于您的输出不是有效的 JSON 而不是 Object/Array 我冒昧地假设这样的事情:
db.collection.aggregate([
{
$addFields: {
"info": {
$concat: [
"$_id",
" ",
"$person.first_name",
" ",
"$person.last_name"
]
}
}
},
{
$project: {
_id: 0,
"person": 0
}
}
])
这会给你:
[
{
"info": "5b84c718bcc9d9114c34bf0a Samuel Sharon"
},
{
"info": "4b86fcdf583643014489261b Jesse James"
},
{
"info": "1b86fcdf583643014489261b Solomon Saunders"
},
{
"info": "2b86fcdf583643014489261b Blake hill"
}
]
你可以在这里看到
推荐阅读
- postgresql - 我想用 postgresql 开发一个 adf fusion web 应用程序项目
- c - 我在接收 C 中的按键时遇到问题
- mysql - docker内部的django-mysql连接错误
- sql-server - Power BI 查询错误:Microsoft SQL:';' 附近的语法不正确。')' 附近的语法不正确
- javascript - 从javascript对象literel返回值
- postgresql - psql 身份验证失败并显示 --host=localhost
- javascript - 未捕获的类型错误:无法读取未定义的属性“1”(即使已定义)
- android - Android gradle 同步:无法获取资源 firebase-measurement-connector-impl-17.0.4-javadoc.jar
- javascript - useReducer/useState 更新太慢
- python - 为什么我们在运行 subprocess.check_output(xyz, shell=True) 后会从终端丢失标准输出?