c# - MongoDb RunCommand 查询嵌入式数组
问题描述
我需要使用“db.RunCommand”从嵌入到集合中的数组中获取项目。我刚开始使用 MongoDB,我很难查看 MongoDB 文档,因为我找不到很多直接使用“RunCommand”方法的示例。
所以如果我有这个例子
[
{
"Brand": {
"Name": "Brand name 1",
"Products": [
{
"Product": "Product 1",
"Price": 20.33
},
// i need to get only the following object
{
"Product": "Product 2",
"Price": 10.33
},
{
"Product": "Product 2",
"Price": 15.33
}
]
}
}
]
那么如何将查询构造到“RunCommand(--query here--)”中以仅获取“Product 2”?所以结果是
{
"Product": "Product 2",
"Price": 10.33
}
如果需要,我使用 C# .net Core 和 MongoDB 驱动程序
解决方案
db.runCommand({
aggregate: "your_collection",
"pipeline": [
{$match: {"_id": ObjectId("5fcd2d3b08c6590a0cd04630")}},
{$project: {
_id: 0, "products": {"$arrayElemAt": ["$a.Brand.Products", 0]}}},
{$project: {"products": {"$arrayElemAt": ["$products", 1]}}}],
cursor: {}})
推荐阅读
- oracle - Docker 中的 Oracle 12c
- python - CSV Python 3中的特殊字符
- javascript - 类型错误:尝试访问函数 onClick 时无法读取未定义的属性“showSubstation”
- jquery - 使用 jQuery 更改样式表文件时使用 cookie 存储当前样式表?
- ios - Viper 架构中的计时器在哪里
- firefox - geckodriver 打开的浏览器没有使用默认设置来运行机器人脚本
- postgresql - Postgres递归json限制
- c++ - 如何将字符串数组更改为字符数组?
- python - 为什么我的端口没有在 docker-compose 中映射?
- xaml - 未设置 UWP PersonPicture 控件背景