我有以下架构,并尝试更新# coding=utf-8
#! /usr/bin/env python
from tablib import Dataset
# load data from the tsv
with open("./test.csv") as fin:
imported_data = Dataset().load(fin.read())
code = input("Please enter Product Code?")
quantity = int(input("How many of these do you need?"))
# code is uniq find the first index
i = imported_data["Code"].index(code)
new_qt = int(imported_data[i][3]) - quantity
# tuple manipulation, so replace the line, item affection not possible
if new_qt >= 0:
imported_data[i] = imported_data[i][:-1] + (str(new_qt),)
else:
imported_data[i] = imported_data[i][:-1] + ("0",)
export = imported_data.tsv
# newline="" avoid add new lines
with open("./test.csv", "w", newline="") as fout:
fout.write(export)
中的值。
answers.counter
代码:
var postSchema = mongoose.Schema({
question : { type: String, required: true },
answers : [answerSchema]
});
var answerSchema = mongoose.Schema({
answe : String,
counter : { type: counterSchema, ref: 'Counter' }
});
var counterSchema = mongoose.Schema({
upvotes : { type: Number, default: 0, min: 0 },
downvotes : { type: Number, default: 0, min: 0 }
});
因此对于Post.findOneAndUpdate(where, { '$inc': { 'answers.$.counter.upvotes': 1 } }, {'new': true, runValidators: true }, (err, post) => {
if (err) return next(err);
console.log('post=' + JSON.stringify(post));
,我尝试了以下其他变体:
1)where
2){ 'answers._id': ObjectId(${req.body.aid}) }
3){ '_id': ObjectId(${req.body.pid}), 'answers._id': ObjectId(${req.body.aid}) }
我收到以下错误:
{ '_id': ObjectId(${req.body.pid}), 'answers._id': ObjectId(${req.body.aid}) 'answers.counter._id': ObjectId(${req.body.cid}) }
这真让我感到困惑,因为通过mongo console,MongoError: exception: The positional operator did not find the match needed from the query. Unexpanded update: answers.$.counter.upvotes
正确地增加了正确的值,这启动了我的整个路径。
提前感谢任何指示。
答案 0 :(得分:0)
事实证明我有一个字符串而不是where
的对象。所以对象形式的#1解决了这个问题:
let where = { '_id': req.body.aid }
以前它是字符串形式(使用$ {}模板):
let where = `{ 'answers._id': ${req.body.aid} }`