Node.js map{';$inc';:{tags.Key:1}中的增量值使nodejs和mongoose中的newkey命名为Key,而不是Key值
Iam正在尝试增加存储为地图的单个标记Node.js map{';$inc';:{tags.Key:1}中的增量值使nodejs和mongoose中的newkey命名为Key,而不是Key值,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,Iam正在尝试增加存储为地图的单个标记 const mongoose = require('mongoose') const validator = require('validator') const question = mongoose.model('question', { text: { type: String, required: true }, answers: [ { type: mo
const mongoose = require('mongoose')
const validator = require('validator')
const question = mongoose.model('question', {
text: {
type: String,
required: true
},
answers: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Answer"
}
],
tags: {
type: Map,
of: Number,
default: 0
}
})
module.exports = question
但是在为增量发送密钥时
<% question.tags.forEach(tag) %>
<% function tag(value,key){ %>
<a href="/question/<%= question._id %>/ <%= key %>">
<%= key %>
</a>
<p>
- <%= value %>
</p>
<% } %>
我明白了
它添加新的键作为键并递增,我以前的键和图中显示的0值相同
app.get('/question/:id/:key',async (req,res,next) =>{
const Key = req.params.key
try{
await question.findByIdAndUpdate(req.params.id,{'$inc':{"tags.Key": 1}},{new: true})
.exec()
res.redirect("back")
} catch(e){
console.log(e)
}
})