node mongodb 根据id给子集合list添加对象 请问应该如何操作
如 给id为1的list添加对象

node mongodb 根据id给子集合list添加对象 请问应该如何操作
如 给id为1的list添加对象

用 Node.js 和 MongoDB 的情况下根据 ID 添加一个对象到给定的子集合,可以使用 MongoDB 的 $push 操作符。
要找到给定的文档,然后使用 $push 操作符把新的对象添加到 list 字段中。
如下
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@cluster.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("reviewer");
collection.updateOne({ id: '1' }, { $push: { list: { name: 'new object' } } }, function(err, res) {
console.log("Document updated");
client.close();
});
});