没有对象解决方案的动态密钥 - MongoDB findOneAndUpdate 调用
Dynamic key without Object solution - MongoDB findOneAndUpdate call
我能够成功地将 { key: value }
结构用于 ...
const findOneAndUpdateUpdate = {
$set: ratingsHistoryObject,
$inc: ratingIncrementObject
}
... 以下代码的一部分。然而,对于..
const findOneAndUpdateFilter = {
'userId': userIds[0],
matchIdPath: { $exists: false },
}
... 部分,不使用大括号。它应该是这样的:...
const findOneAndUpdateFilter = {
'userId': userIds[0],
"ratingsHistory.squash.60003e9267684effa0f56ffb": { $exists: false },
}
...但随后动态 matchIdPath
。当 { key: value }
方法在这里不起作用时,我怎么能意识到这一点,因为没有花括号开始 matchIdPath
?
完整代码摘录:
// Other code
matchesData.forEach(async match => {
// Other code
const matchIdPath = `ratingsHistory.${match.matchType}.${match._id}`
var ratingsHistoryObject = {}
var ratingsHistoryObjectKey = 'ratingsHistory.' + match.matchType + '.' + match._id
var ratingsHistoryObjectValue = difference_result
ratingsHistoryObject[ratingsHistoryObjectKey] = ratingsHistoryObjectValue
var ratingIncrementObject = {}
var ratingIncrementObjectKey = 'ratings.' + match.matchType
var ratingIncrementObjectValue = difference_result
ratingIncrementObject[ratingIncrementObjectKey] = ratingIncrementObjectValue
const findOneAndUpdateFilter = {
'userId': userIds[0],
matchIdPath: { $exists: false },
}
const findOneAndUpdateUpdate = {
$set: ratingsHistoryObject,
$inc: ratingIncrementObject
}
const usersResult = await usersCollection.findOneAndUpdate(findOneAndUpdateFilter, findOneAndUpdateUpdate);
您可以使用 computed property name syntax:
const findOneAndUpdateFilter = {
'userId': userIds[0],
[matchIdPath]: { $exists: false },
}
示例:
let key = "ratingsHistory.squash.60003e9267684effa0f56ffb";
let obj = { [key]: { $exists: false } };
console.log(obj);
我能够成功地将 { key: value }
结构用于 ...
const findOneAndUpdateUpdate = {
$set: ratingsHistoryObject,
$inc: ratingIncrementObject
}
... 以下代码的一部分。然而,对于..
const findOneAndUpdateFilter = {
'userId': userIds[0],
matchIdPath: { $exists: false },
}
... 部分,不使用大括号。它应该是这样的:...
const findOneAndUpdateFilter = {
'userId': userIds[0],
"ratingsHistory.squash.60003e9267684effa0f56ffb": { $exists: false },
}
...但随后动态 matchIdPath
。当 { key: value }
方法在这里不起作用时,我怎么能意识到这一点,因为没有花括号开始 matchIdPath
?
完整代码摘录:
// Other code
matchesData.forEach(async match => {
// Other code
const matchIdPath = `ratingsHistory.${match.matchType}.${match._id}`
var ratingsHistoryObject = {}
var ratingsHistoryObjectKey = 'ratingsHistory.' + match.matchType + '.' + match._id
var ratingsHistoryObjectValue = difference_result
ratingsHistoryObject[ratingsHistoryObjectKey] = ratingsHistoryObjectValue
var ratingIncrementObject = {}
var ratingIncrementObjectKey = 'ratings.' + match.matchType
var ratingIncrementObjectValue = difference_result
ratingIncrementObject[ratingIncrementObjectKey] = ratingIncrementObjectValue
const findOneAndUpdateFilter = {
'userId': userIds[0],
matchIdPath: { $exists: false },
}
const findOneAndUpdateUpdate = {
$set: ratingsHistoryObject,
$inc: ratingIncrementObject
}
const usersResult = await usersCollection.findOneAndUpdate(findOneAndUpdateFilter, findOneAndUpdateUpdate);
您可以使用 computed property name syntax:
const findOneAndUpdateFilter = {
'userId': userIds[0],
[matchIdPath]: { $exists: false },
}
示例:
let key = "ratingsHistory.squash.60003e9267684effa0f56ffb";
let obj = { [key]: { $exists: false } };
console.log(obj);