Node.js 使用nodejs//throw new TypeError(';更新文档需要原子运算符';)更新mongoDB集合;
因此,我可以使用nodejs/mongoDB创建一个集合 使用此代码Node.js 使用nodejs//throw new TypeError(';更新文档需要原子运算符';)更新mongoDB集合;,node.js,mongodb,Node.js,Mongodb,因此,我可以使用nodejs/mongoDB创建一个集合 使用此代码 // create a collection and inserting the object dbo.collection('employees').insertOne(myobj, function(err, res) { //if there is an error throw error message if (err) throw err; // print out if collect
// create a collection and inserting the object
dbo.collection('employees').insertOne(myobj, function(err, res) {
//if there is an error throw error message
if (err) throw err;
// print out if collection is created
console.log(colors.green("Collection created ✓"),'\n','\n',companyMotto, '-',
subDomain,'\n','\n');
//close the db
db.close();
});
我试图找出一个代码来更新集合作为一个整体,如果他们的集合已经存在,使用这个
// create a collection and inserting the object
dbo.collection('employees').updateOne(myobj, function(err, res) {
//if there is an error throw error message
if (err) throw err;
// print out if collection is updated
console.log(colors.green("Collection updated ✓"),'\n','\n',companyMotto, '-',
subDomain,'\n','\n');
//close the db
db.close();
});
但是我得到一个错误“…抛出新的TypeError('更新文档需要原子运算符');”
如果您想创建一个集合,您只需调用
createCollection
,任何帮助都将不胜感激insertOne
用于将文档插入集合,而updateOne
用于更新集合中的特定文档
更新文档需要原子运算符
表示未提供更新操作。所以mongodb不知道您希望如何更新文档。e、 g.$set
更新值,$push
将值推入数组
您可以参考nodejs文档,了解如何调用updateOne
。
updateOne(过滤器、更新、选项、回调)
见: