Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

javascript - Get position with variable and then modify specific object in mongodb

How can I make this query use my variable instead of the hard coded 0?

let pos = 0;

{$set: {'answers.0.acknowledged': data}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use ES6 String Interpolation and Computed Property Names to build the key for $set:

let pos = 0;
let data = 'some data';
let q = {$set: {[`answers.${pos}.acknowledged`]: data}}
console.log(q);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...