如何更新猫鼬中的数字数组
How to update a array of numbers in mongoose
我无法在 MongoDB (Mongoose) 中更新文档内的数组。这是我对股票模型的定义。
const ticker = new mongoose.Schema({
ticker: String,
Price: Number,
Amount: Number,
PastPrices: [Number]
});
export const stock = mongoose.model("Stocks", ticker);
这是 MongoDB
中的一份文档
{
"_id": {
"$oid": "61e5d0e1dfda4d7c85dc8fe2"
},
"PastPrices": [
2
],
"ticker": "TSLA",
"Price": 2,
"Amount": 0,
"__v": 0
}
我在 mongoose 中 运行 并且 PastPrices 没有更新。我希望能够每隔几秒将其推送到此数组中,然后将其渲染到图表中
stock.updateOne({ticker: "TSLA"},
{ $push: { PastPrices:1}}
);
我没有收到任何错误,只是没有更新
您的查询是正确的,但是为了让查询return更新数据,您需要传递额外的配置{ new: true }
。如果不传,查询会更新数据,但是会return旧数据
stock.updateOne(
{ ticker: "TSLA" },
{ $push: { PastPrices:1 } },
{ new: true }
);
updateOne
returns a Query, which doesn't execute your update immediately. From the Mongoose guide for Queries 它指出:
A mongoose query can be executed in one of two ways. First, if you pass in a callback function, Mongoose will execute the query asynchronously and pass the results to the callback.
A query also has a .then() function, and thus can be used as a promise.
意味着你可以传递一个回调函数,就像在 documentation for executing Queries:
const Person = mongoose.model('Person', yourSchema);
// find each person with a last name matching 'Ghost', selecting the `name` and > `occupation` fields
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
if (err) return handleError(err);
// Prints "Space Ghost is a talk show host".
console.log('%s %s is a %s.', person.name.first, person.name.last,
person.occupation);
});
你也可以使用then
or exec
if you want to use Promises. From the Mongoose documentation for promises:
const query = Band.findOne({name: "Guns N' Roses"});
assert.ok(!(query instanceof Promise));
// A query is not a fully-fledged promise, but it does have a `.then()`.
query.then(function(doc) {
// use doc
});
// `.exec()` gives you a fully-fledged promise
const promise = Band.findOne({name: "Guns N' Roses"}).exec();
assert.ok(promise instanceof Promise);
promise.then(function (doc) {
// use doc
});
如果在异步方法中,您还可以await
查询:
await stock.updateOne({ticker: "TSLA"},
{ $push: { PastPrices:1}}
);
我无法在 MongoDB (Mongoose) 中更新文档内的数组。这是我对股票模型的定义。
const ticker = new mongoose.Schema({
ticker: String,
Price: Number,
Amount: Number,
PastPrices: [Number]
});
export const stock = mongoose.model("Stocks", ticker);
这是 MongoDB
中的一份文档{
"_id": {
"$oid": "61e5d0e1dfda4d7c85dc8fe2"
},
"PastPrices": [
2
],
"ticker": "TSLA",
"Price": 2,
"Amount": 0,
"__v": 0
}
我在 mongoose 中 运行 并且 PastPrices 没有更新。我希望能够每隔几秒将其推送到此数组中,然后将其渲染到图表中
stock.updateOne({ticker: "TSLA"},
{ $push: { PastPrices:1}}
);
我没有收到任何错误,只是没有更新
您的查询是正确的,但是为了让查询return更新数据,您需要传递额外的配置{ new: true }
。如果不传,查询会更新数据,但是会return旧数据
stock.updateOne(
{ ticker: "TSLA" },
{ $push: { PastPrices:1 } },
{ new: true }
);
updateOne
returns a Query, which doesn't execute your update immediately. From the Mongoose guide for Queries 它指出:
A mongoose query can be executed in one of two ways. First, if you pass in a callback function, Mongoose will execute the query asynchronously and pass the results to the callback.
A query also has a .then() function, and thus can be used as a promise.
意味着你可以传递一个回调函数,就像在 documentation for executing Queries:
const Person = mongoose.model('Person', yourSchema); // find each person with a last name matching 'Ghost', selecting the `name` and > `occupation` fields Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) { if (err) return handleError(err); // Prints "Space Ghost is a talk show host". console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation); });
你也可以使用then
or exec
if you want to use Promises. From the Mongoose documentation for promises:
const query = Band.findOne({name: "Guns N' Roses"}); assert.ok(!(query instanceof Promise)); // A query is not a fully-fledged promise, but it does have a `.then()`. query.then(function(doc) { // use doc }); // `.exec()` gives you a fully-fledged promise const promise = Band.findOne({name: "Guns N' Roses"}).exec(); assert.ok(promise instanceof Promise); promise.then(function (doc) { // use doc });
如果在异步方法中,您还可以await
查询:
await stock.updateOne({ticker: "TSLA"},
{ $push: { PastPrices:1}}
);