我正在尝试更新一个MongoDb
集合,该集合具有一个名为items
的文档数组。为此,我正在使用express
和mongoose
框架。
这是我的schema
的样子:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
let invoices = new Schema({
vendor: { type: String },
invoiceDate: { type: Date, default: Date.now },
expectedDate: { type: Date, default: Date.now },
billingAddress: { type: String },
contactPerson: { type: String },
items: [
{
itemID: Schema.Types.ObjectId,
qty: { type: Number },
unitPrice: { type: Number },
linePrice: { type: Number }
}
],
totalPrice: { type: Number }
});
module.exports = mongoose.model("invoices", invoices);
我想通过首先找到特定文档的ID ,然后相应地更新items
来更新特定文档。
这是我到目前为止尝试过的,并且我不知道下一步要更新数组items
。
//end-point-4
Routes.route('/update/:id').post((req, res) => {
invoicesDB.findById(req.params.id, (err, invoice) => {
if(!invoice){
res.status(404).send('Not found!');
}else{
invoice.vendor = req.body.vendor;
invoice.invoiceDate = req.body.invoiceDate;
invoice.expectedDate = req.body.expectedDate;
invoice.billingAddress = req.body.billingAddress;
invoice.contactPerson = req.body.contactPerson;
//how to update items
invoice.items = req.body.items; //this I guess, gives the answer but not sure.
invoice.totalPrice = req.body.totalPrice;
}
});
});
PS:我不想更新items
中的某个项目。我想要的是用给定的值更新数组中的每个项目。
举个例子,假设用户只想更新items
中的特定项目,因此只应更新该特定项目。
答案 0 :(得分:1)
您可以按照以下说明直接进行更新:
class Pieces():
def __init__(self, empassant=(-5,-5)):
super().__init__()
self.empassant=empassant
self.game=Game()
self.white=White()
self.black=Black()
class Game ():
def __init__(self):
super().__init__()
self.white=White()
self.black=Black()
self.turn="WHITE"
self.pieces=Pieces()
self.player=self.white
self.opponent=self.black
self.person=self.white
def move(self):
if self.turn=="BLACK":
self.player=self.black
self.opponent=self.white
else:
self.player=self.white
self.opponent=self.black
self.person=self.opponent
self.movecalc()#calculates opponents moves
self.person=self.player#calculates players moves
self.movecalc()
def movecalc(self):
# it was initially: pieces=Pieces()
答案 1 :(得分:0)
在猫鼬上,有一个'$'表示一个数组,您可以这样做:
$ factor 3 4 5
3: 3
4: 2 2
5: 5