MongoDB - 如何用新值替换空值
MongoDB - How to replace from null value with new values
我正在使用MongoDB中的聚合方法查询数据,但我遇到了问题。具体来说,我想用 price_option
字段的值替换 price
字段的值 (条件:如果此字段值为空) model
字段中的第一项,其中 stock_count
大于 0。我该怎么做?
我使用聚合查询集合的代码:
const doc = await ProductModel.aggregate([
...
{
$group: {
_id: '$_id',
price: { $first: "$price" },
model: {
$addToSet: {
price_option: '$model.priceOption',
currency_unit: '$model.currencyUnit',
stock_count: '$model.stockCount'
}
}
}
},
{
$project: {
_id: 0,
id: '$_id',
price: 1,
model: 1
}
}
])
我在集合中创建的示例数据:
{
"price": null,
"model": [
{
"price_option": "200",
"currency_unit": "dollar",
"stock_count": 5
},
{
"price_option": "350",
"currency_unit": "dollar",
"stock_count": 0
},
{
"price_option": "400",
"currency_unit": "dollar",
"stock_count": 2
},
]
}
]
我想要的输出是:
{
"price": "200", <= it would be replace by the first item of remaining items where stock_count greater than 0
"model": [
{
"price_option": "200",
"currency_unit": "dollar",
"stock_count": 5
},
{
"price_option": "350",
"currency_unit": "dollar",
"stock_count": 0
},
{
"price_option": "400",
"currency_unit": "dollar",
"stock_count": 2
},
]
}
$set
- 创建 price_model
字段。
1.1。 $filter
过滤 model
数组中的文档,其中 stock_count
$get
0.
1.2。 first
从 1.1 中的结果中获取第一个文档。
$project
- 修饰输出文档。使用 price_model.price_option
.
分配 price
字段
db.collection.aggregate([
{
$set: {
price_model: {
$first: {
"$filter": {
"input": "$model",
"cond": {
$gt: [
"$$this.stock_count",
0
]
}
}
}
}
}
},
{
$project: {
_id: 0,
id: "$_id",
price: "$price_model.price_option",
model: 1
}
}
])
对于MongoDB 4.2版本,您可以使用$arrayElementAt
获取数组的第一个元素。
db.collection.aggregate([
{
$set: {
price_model: {
$arrayElemAt: [
{
"$filter": {
"input": "$model",
"cond": {
$gt: [
"$$this.stock_count",
0
]
}
}
},
0
]
}
}
},
{
$project: {
_id: 0,
id: "$_id",
price: "$price_model.price_option",
model: 1
}
}
])
我正在使用MongoDB中的聚合方法查询数据,但我遇到了问题。具体来说,我想用 price_option
字段的值替换 price
字段的值 (条件:如果此字段值为空) model
字段中的第一项,其中 stock_count
大于 0。我该怎么做?
我使用聚合查询集合的代码:
const doc = await ProductModel.aggregate([
...
{
$group: {
_id: '$_id',
price: { $first: "$price" },
model: {
$addToSet: {
price_option: '$model.priceOption',
currency_unit: '$model.currencyUnit',
stock_count: '$model.stockCount'
}
}
}
},
{
$project: {
_id: 0,
id: '$_id',
price: 1,
model: 1
}
}
])
我在集合中创建的示例数据:
{
"price": null,
"model": [
{
"price_option": "200",
"currency_unit": "dollar",
"stock_count": 5
},
{
"price_option": "350",
"currency_unit": "dollar",
"stock_count": 0
},
{
"price_option": "400",
"currency_unit": "dollar",
"stock_count": 2
},
]
}
]
我想要的输出是:
{
"price": "200", <= it would be replace by the first item of remaining items where stock_count greater than 0
"model": [
{
"price_option": "200",
"currency_unit": "dollar",
"stock_count": 5
},
{
"price_option": "350",
"currency_unit": "dollar",
"stock_count": 0
},
{
"price_option": "400",
"currency_unit": "dollar",
"stock_count": 2
},
]
}
$set
- 创建price_model
字段。1.1。
$filter
过滤model
数组中的文档,其中stock_count
$get
0.1.2。
first
从 1.1 中的结果中获取第一个文档。
分配$project
- 修饰输出文档。使用price_model.price_option
.price
字段
db.collection.aggregate([
{
$set: {
price_model: {
$first: {
"$filter": {
"input": "$model",
"cond": {
$gt: [
"$$this.stock_count",
0
]
}
}
}
}
}
},
{
$project: {
_id: 0,
id: "$_id",
price: "$price_model.price_option",
model: 1
}
}
])
对于MongoDB 4.2版本,您可以使用$arrayElementAt
获取数组的第一个元素。
db.collection.aggregate([
{
$set: {
price_model: {
$arrayElemAt: [
{
"$filter": {
"input": "$model",
"cond": {
$gt: [
"$$this.stock_count",
0
]
}
}
},
0
]
}
}
},
{
$project: {
_id: 0,
id: "$_id",
price: "$price_model.price_option",
model: 1
}
}
])