gpt4 book ai didi

mongodb - 从对象数组到数组

转载 作者:行者123 更新时间:2023-12-04 10:41:26 26 4
gpt4 key购买 nike

我有疑问db.collection('image').find({ category: "concept art"}, { projection: _id: 0, imgUrl:1 }).toArray()并有一个输出:

[
{
"imgUrl": "images\\2020-01-23T14-41-48.158Z-abandoned.jpg"
},
{
"imgUrl": "images\\2020-01-25T01-45-14.880Z-pIMG_6443.jpg"
}
]

我想要这样的输出:
[
"images\\2020-01-23T14-41-48.158Z-abandoned.jpg",
"images\\2020-01-25T01-45-14.880Z-pIMG_6443.jpg"
]

我在哪里可以找到有关此的信息或如何实现此目标?

最佳答案

您需要尝试 aggregation为了那个原因 :

db.collection.aggregate([
/** match docs based on criteria same as find */
{ $match: { "category": "concept art" } },
/** group all docs and push imageUrls to an array */
{ $group: { _id: '', imgUrl: { $push: '$imgUrl' } } },
/** remove _id from final doc */
{ $project: { _id: 0 } }])

采集数据:
/* 1 */
{
"_id" : ObjectId("5e2bc27cdc95d08ab83819e8"),
"category" : "concept art",
"isImage" : true,
"imgUrl" : "images\\2020-01-23T14-41-48.158Z-abandoned.jpg"
}

/* 2 */
{
"_id" : ObjectId("5e2bc27cdc95d08ab83819e9"),
"category" : "concept art",
"isImage" : true,
"imgUrl" : "images\\2020-01-25T01-45-14.880Z-pIMG_6443.jpg"
}

输出:
/* 1 */
{
"imgUrl" : [
"images\\2020-01-23T14-41-48.158Z-abandoned.jpg",
"images\\2020-01-25T01-45-14.880Z-pIMG_6443.jpg"
]
}

关于mongodb - 从对象数组到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59906272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com