Mongoose:findOneAndUpdate 不更新现有字段 [英] Mongoose: findOneAndUpdate does not update an existing field
问题描述
我正在尝试每分钟获取一个 Web API 数据到 MongoDB,保存并在有任何更改时更新.问题是 volume
(每个字段)字段仅在新的 date
添加到 ChildSchemaData
时更新(在新的交易日开始后发生)> - 子文档.所有这些数据都用于在前端构建每日(不是每小时/分钟)股票图表.我正在尝试从数据库中流式传输它,但是由于 volume
(以及子文档中的所有其他字段)没有在数据库中更新,前端没有任何变化.我正在尝试实现更改 Streams MongoDB(来自数据库的数据流),所以我坚持从数据库中获取数据.任何帮助表示赞赏.谢谢!
I am trying to fetch every minute a web API data to MongoDB, save it and update when there is any change. The problem is the volume
(every field) field is updated only (happens after a new trading day starts) when the new date
is added to ChildSchemaData
- subdocumet. All this data is used to build a daily (NOT hourly/minute ) stock chart on front-end. And I am trying to stream it from database but since the volume
(and all other fields in subdocument) is not updated in database nothing is changing on front-end. I am trying to implemet change Streams MongoDB (data stream from database) so I insist on getting data from database. Any help is appreciated. Thank you!
Edit1:$addToSet
运算符向数组添加一个值,除非该值已经存在,在这种情况下 $addToSet
什么都不做到那个数组.看起来这就是问题所在.我想知道是否有不同的运算符可以更新已经存在的值.
Edit1: The $addToSet
operator adds a value to an array unless the value is already present, in which case $addToSet
does nothing to that array. It looks like that is the problem. I am wondering if there is a different operator that would update the value that is already present.
Edit2:我正在使用以下语法,但不确定它是否正确
Edit2: I was playing around with the following syntax but not sure if it is correct
const update = {
$addToSet: { data: webApiData },
$set: { 'data[0].$.volume': webApiData[0].volume },
};
因为它进入相同的path
,所以它不会工作
because it is getting into the same path
it is not going to work
控制器
const creatStock = async (symbol, webApiData) => {
try {
const query = { symbol };
const update = { $addToSet: { data: webApiData } };
const options = { upsert: true, new: true };
await Stock.findOneAndUpdate(query, update, options);
} catch (ex) {
console.log(`creatStock error: ${ex}`.red);
}
};
架构
const ChildSchemaData = new mongoose.Schema({
_id: false,
date: { type: mongoose.Types.Decimal128 },
open: { type: mongoose.Types.Decimal128 },
high: { type: mongoose.Types.Decimal128 },
low: { type: mongoose.Types.Decimal128 },
close: { type: mongoose.Types.Decimal128 },
volume: { type: mongoose.Types.Decimal128 },
});
const ParentSchemaSymbol = new mongoose.Schema({
symbol: {
type: String,
},
// Array of subdocuments
data: [ChildSchemaData],
});
推荐答案
This solution was provided by srinivasy at the following link link
const creatStock = async (symbol, webApiData) => {
try {
// reversed array
const webApiDataReversed = webApiData.reverse();
const query = { symbol };
const position = await Stock.findOne(query);
if (!position) {
console.log('New stock'.green);
return Stock.create({
symbol,
data: webApiDataReversed,
});
}
await Stock.bulkWrite([
{
updateOne: {
filter: query,
update: { $pop: { data: 1 } },
},
},
{
updateOne: {
filter: query,
update: {
$addToSet: {
data: webApiDataReversed,
},
},
},
},
]);
} catch (ex) {
console.log(`creatStock error: ${ex}`.red);
}
};
这篇关于Mongoose:findOneAndUpdate 不更新现有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!