更新猫鼬数组

时间:2020-05-26 14:51:32

标签: node.js mongodb mongoose discord.js

所以基本上我有这个,我正在尝试更新数组的//In AutoComplete adapter public MutableLiveData<String> getQuantity() { if (mQuantity == null) { mQuantity = new MutableLiveData<>(); } return mQuantity; } //In handler constructor mAutoCompleteAdapter.getQuantity().observe( (LifecycleOwner) activity, new Observer<String>() { @Override public void onChanged(String s) { mQuantity = s; refreshSpans(); } } ); private void refreshSpans() { Editable s = mTextView.getText(); clearEditTextSpan(s); if (!mQuantity.isEmpty()) { //Set span int indexStart = mAutoCompleteAdapter.getQuantityIndex(); s.setSpan( new BackgroundColorSpan( Color.RED), indexStart, indexStart + mQuantity.length(), Spanned.SPAN_MARK_MARK ); } } private void clearEditTextSpan(Editable s) { s.setSpan( new BackgroundColorSpan( Color.TRANSPARENT), 0, s.length(), Spanned.SPAN_MARK_MARK ); } 部分。 但是,我尝试的所有操作均无济于事。我也尝试过STATUS。我正在尝试通过数字识别数组中的特定项目,然后更新该特定数组的状态部分 (很抱歉,格式化,我还不知道该如何在网站上进行操作……)(完整的代码可以在这里找到:https://sourceb.in/0811b5f805

代码

findOneAndUpdate

模式

const ticketObj = {
  number: placeholderNumber,
  userID: message.author.id,
  message: m.content,
  status: 'unresolved'
}

let tnumber = parseInt(args[1])
let statuss = "In Progress"
await Mail.updateOne({
  "number": tnumber
}, { $set: { "status": statuss } })

1 个答案:

答案 0 :(得分:0)

您需要使用类似Mail.updateOne({"guildID": message.guild.id}, {$set: {`tickets.${tnumber}.status`: statuss}})

或针对数组中的所有对象:

Mail.updateOne({"guildID": message.guild.id}, {$set: {'tickets.$[].status': statuss}})

此外,您还需要为tickets创建一个架构,如docs中所述:

one important reason to use subdocuments is to create a path where there would otherwise not be one to allow for validation over a group of fields