我在Nodejs中有一个使用mongoose的应用程序。 我有一个集合,在每个15个记录的一个字段中保存相同的值,但这个值是我之前创建的一个长字符串。 现在我希望每15张照片将这些字段值(长字符串)更改为1..n之类的增量数字:
'qwerasdfzxcvqwerasdfzxcv' => '1',
'qwerasdfzxcvqwerasdfzxcv' => '1',
(13 more records)...
'qazwsxedcrfvtgbyhnuhnujm' => '2',
'qazwsxedcrfvtgbyhnuhnujm' => '2',
(13 more records)...
MongoDB的JSON结果:
[{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_wasp_565","description":"","answer":"Bee","__v":0},{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_Pompilid_wasp_007","description":"","answer":"Wasp","__v":0},{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"wasp_248","description":"","answer":"Wasp","__v":0},...,{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_wasp_555","description":"","answer":"Moth/Butterfly","__v":0},{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQxMTYzNzJ9.jTQ06D5nw_xuCP0q0GVzaMmi3XipeNsf_RX5tAzenh8","category":"Wasp","photo":"A_wasp_565","description":"","answer":"Wasp","__v":0},{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQxMTYzNzJ9.jTQ06D5nw_xuCP0q0GVzaMmi3XipeNsf_RX5tAzenh8","category":"Wasp","photo":"A_Pompilid_wasp_007","description":"","answer":"Wasp","__v":0},{"ID":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQxMTYzNzJ9.jTQ06D5nw_xuCP0q0GVzaMmi3XipeNsf_RX5tAzenh8","category":"Wasp","photo":"A_wasp_555","description":"","answer":"Wasp","__v":0}]}
我想在上面更新:
[{"ID":"1","category":"Wasp","photo":"A_wasp_565","description":"","answer":"Bee","__v":0},{"ID":"1","category":"Wasp","photo":"A_Pompilid_wasp_007","description":"","answer":"Wasp","__v":0},{"ID":"1","category":"Wasp","photo":"wasp_248","description":"","answer":"Wasp","__v":0},...,{"ID":"1","category":"Wasp","photo":"A_wasp_555","description":"","answer":"Moth/Butterfly","__v":0},{"ID":"2","category":"Wasp","photo":"A_wasp_565","description":"","answer":"Wasp","__v":0},{"ID":"2","category":"Wasp","photo":"A_Pompilid_wasp_007","description":"","answer":"Wasp","__v":0},{"ID":"2","category":"Wasp","photo":"A_wasp_555","description":"","answer":"Wasp","__v":0}]}
通过这种方式,所有这15条记录(已经具有相同的值但很长)将具有相同的数字但是可读的值,而不是长字符串。
我的问题是如何进行查询呢? (mongoose命令)
这是节点中的模型模块(ID是我要更改的字段):
var Schema = mongoose.Schema;
var SurveyAnswersSchema = new Schema({
_id: {type: String},
ID: {type: String},
answer: { type: String},
description: {type: String},
category: { type: String, enum: ['Bee', 'Beetle', 'Fly', 'Moth/Butterfly', 'Wasp' ] },
});
module.exports = mongoose.model('surveyanswers', SurveyAnswersSchema);
我应该在这写什么? (控制器模块)
var SurveyResults = require('../models/surveyResults');
module.exports.reportAnswers = function (callback) {
**// instead of this line what should I write?**
SurveyResults.find({ID: 'this.ID'},{ID: number}, callback);
};
感谢您的帮助。
答案 0 :(得分:0)
我已经解决了这个问题。 这是我的代码。也许对某人有用:
app.get('/surveyResultsUpdate', function(req, res, next){
surveyResults.surveyResultsUpdate(function(err, answers){
if (err)
return next(err);
var num =0;
var resultsIdSet = new Set();
answers.forEach(function(answer) {
resultsIdSet.add(answer.Id);
});
var newResults = answers;
var user_answer_dict = new Map();
var counter = 0;
for (var Id of resultsIdSet){
num += 1;
for ( var j in newResults) {
if (Id === newResults[j].Id){
counter+=1;
user_answer_dict.set(counter, newResults[j]);
newResults[j]._id = answers[j]._id;
newResults[j].Id = num;
newResults[j].answer = answers[j].answer;
newResults[j].photo = answers[j].photo;
newResults[j].description = answers[j].description;
newResults[j].category = answers[j].category;
}
}
}
res.json(newResults);
});
});
结果: