如何使用 mongodb C# 驱动程序获取子文档集合?
How to fetch collection of subdocument using mongodb C# driver?
我有以下 MongoDB 文档:
{
"_id": {
"$oid": "5fbfa0005c15aaf2eac69ba6"
},
"PostMedia": [
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}
]
},
{
"_id": {
"$oid": "5fbfa0485c15aaf2eac69ba7"
},
"PostMedia": [
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
}
我想使用 MongoDB C# 驱动程序将所有 PostMedia
提取到单个数组。
这是预期的结果:
[
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}, {
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
我曾尝试使用组聚合函数,但它返回了一个数组。
我收到的结果:
PostMedia:[{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}],[
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
目前我写的C#代码如下:
var group = new[]
{
new BsonDocument("$group",
new BsonDocument
{
{ "_id", BsonNull.Value },
{ "PostMedia", new BsonDocument("$push", "$PostMedia") }
})
};
var result = collection.Aggregate<MediaList>(group).FirstOrDefault();
return list;
}
有没有办法通过将它们合并为一个数组来获取子文档。
你必须 $unwind
在 $group
,
之前
$unwind
解构PostMedia
数组
$replaceRoot
将 PostMedia
对象替换为根目录
$unset
删除 _id
字段
{ $unwind: "$PostMedia" },
{
$group: {
_id: null,
PostMedia: { $push: "$PostMedia" }
}
},
{ $unset: "_id" } // remove field
C#:
new BsonArray
{
new BsonDocument("$unwind", "$PostMedia"),
new BsonDocument("$group",
new BsonDocument
{
{ "_id", BsonNull.Value },
{ "PostMedia",
new BsonDocument("$push", "$PostMedia") }
})
new BsonDocument("$unset", "_id")
}
我有以下 MongoDB 文档:
{
"_id": {
"$oid": "5fbfa0005c15aaf2eac69ba6"
},
"PostMedia": [
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}
]
},
{
"_id": {
"$oid": "5fbfa0485c15aaf2eac69ba7"
},
"PostMedia": [
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
}
我想使用 MongoDB C# 驱动程序将所有 PostMedia
提取到单个数组。
这是预期的结果:
[
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}, {
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
我曾尝试使用组聚合函数,但它返回了一个数组。
我收到的结果:
PostMedia:[{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}],[
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
目前我写的C#代码如下:
var group = new[]
{
new BsonDocument("$group",
new BsonDocument
{
{ "_id", BsonNull.Value },
{ "PostMedia", new BsonDocument("$push", "$PostMedia") }
})
};
var result = collection.Aggregate<MediaList>(group).FirstOrDefault();
return list;
}
有没有办法通过将它们合并为一个数组来获取子文档。
你必须 $unwind
在 $group
,
$unwind
解构PostMedia
数组$replaceRoot
将PostMedia
对象替换为根目录$unset
删除_id
字段
{ $unwind: "$PostMedia" },
{
$group: {
_id: null,
PostMedia: { $push: "$PostMedia" }
}
},
{ $unset: "_id" } // remove field
C#:
new BsonArray
{
new BsonDocument("$unwind", "$PostMedia"),
new BsonDocument("$group",
new BsonDocument
{
{ "_id", BsonNull.Value },
{ "PostMedia",
new BsonDocument("$push", "$PostMedia") }
})
new BsonDocument("$unset", "_id")
}