node.js - 使用节点 js 中的 findOneAndUpdate 更新数组中的对象
问题描述
我在 mongodb 中有这些数据,我想用一个查询更新它:
{
"_id" : ObjectId("zzzzz6e57fss8ssb9cssbsss"),
"user" : "zzzz",
"data" : [
{
"_id" : ObjectId("zzzzz6e57ffe8b2b9c77b6b9"),
"me" : "LLL"
},
{
"_id" : ObjectId("5b8ab719406d7235240f4338"),
"me" : "III"
}
]
"__v" : NumberInt(0)
}
我想 通过findOneAndUpdate更改'me' : 'III' 此代码不起作用。
var ObjectID = require('mongodb').ObjectID;
var o_id = new ObjectID("5b8ab719406d7235240f4338");
array.findOneAndUpdate
(
{ 'data._id' : o_id },
{ $set:{'me' : "Stackoverflow" } },
function (error, success)
{
if (error) console.log(error);
if(success == null )
console.log("nullllllllllllllllllllllll");
console.log(success);
}
);
解决方案
var ObjectID = require('mongodb').ObjectID;
var o_id = new ObjectID("5b8ab719406d7235240f4338");
array.findOneAndUpdate
(
{ 'data._id' : o_id },
{ $set:{'data.$.fr' : "Stackoverflow" } },
function (error, success)
{
if (error) console.log(error);
if(success == null )
console.log("nullllllllllllllllllllllll");
console.log(success);
}
);
推荐阅读
- python - 有没有办法每 30 行循环一次矩阵/数组/df 以返回 scipy.stats.describe
- c# - 如何创建 EF 迁移类
- cypress - 在测试运行器中看不到新创建的文件夹中的测试 - cypress
- caching - HBase 缓存与外部缓存
- javascript - sequelize findAll 不检查当前用户是否喜欢帖子
- javascript - 打印所有行,包括被分页隐藏的行
- c++ - 如何使用函数 std::min() 计算最小值?
- reactjs - Easy-peasy - 无法返回新状态
- mysql - 生成列以在 mysql mariadb 中添加带有计数器的时间戳列
- go - 如何防止递归函数中的死锁?