node.js - 替换文档不得包含原子运算符

标签 node.js mongodb mongoose

我有这个功能来保存新记录或更新现有项目:

async function saveEmploy(c){
let employ=new collaboratore();
employ=c;
let docs=c.documenti.length>0?c.documenti:undefined;
if(docs!==undefined){
    let oldDocs=docs.map(d=>{if (d._id) d;});
    oldDocs.forEach(async d=>{
        let oldDoc=await documento.findOneAndReplace({_id:doc._id},doc,{upsert:true});
        employ.documenti.push(oldDoc._id);
    });
    let newDocs=docs.map(d=>{if(!d._id)d;});
    newDocs.forEach(async d=>{
        let newDoc=await d.save();
        employ.documenti.push(newDoc._id);
    });
}

let savedEmploy=null;
if(c._id){
    savedEmploy=await collaboratore.findOneAndReplace({_id:c._id},employ,{new:true,upsert:true});
}
else
    savedEmploy=await employ.save();
console.log('saved');
return savedEmploy;
}

这些是架构:

const collaboratoreschema =new schema({
   dataregistrazione:{type:Date,default:Date.now},
   nominativo:nominativoschema,
   localitanascita:{type:String,required:true,trim:true},
   provincianascita:{type:String,required:true,trim:true},
   datanascita:{type:Date,required:true},
   indirizzoresidenza:{type:indirizzoEmbeddedSchema,required:true},
   indirizzodomicilio:{type:indirizzoEmbeddedSchema,set:v=>!v || v.length===0?undefined:v},
   telefoni:{type:[telefonoschema],required:true},
   indirizziemail:{type:[require('../email.schema.js')],set:v=>!v || v.length===0?undefined:v},
   codicefiscale:{type:String,required:true,uppercase:true,trim:true,match:/^[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z]$/},
   note:{type:String,trim:true,set:v=>!v || v===''?undefined:v},
   immagine:imageEmbeddedSchema,
   documenti:[{type:schema.Types.ObjectId,ref:'tbFiles'}],
   datainiziorapporto:{type:Date,required:true},
   datafinerapporto:Date,
   riferimentoazienda:{type:schema.Types.ObjectId,ref:'tbFornitori',set:v=>!v || v==={} || v===''?undefined:v},
   attivo:{type:Boolean,default:true}
},{timestamps:true,useNestedStrict: true,collection:'tbCollaboratori'});

当我执行 findOneAndReplace 函数时,出现此错误:

The replacement document must not contain atomic operators.

我不明白我的错误在哪里。

感谢您的帮助

最佳答案

我敢打赌,您的更新文档,无论是 doc 还是 employ,都包含更新操作 ($set: {...})。
顾名思义,findOneAndReplace 替换文档的内容,因此此处不允许更新运算符。
您很可能需要使用 findOneAndUpdate此处或确保您的更新文档包含完整的新记录。
引用这篇文章What's the difference between findOneAndUpdate and findOneAndReplace?和这篇文章MongoDB findOneAndDelete findOneAndUpdate findOneAndReplace也了解详细信息。

关于node.js - 替换文档不得包含原子运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66479023/

相关文章:

javascript - 分配属性值在 Mongoose 中不起作用

javascript - 使用 Promise/Bluebird 进行并行 Mongoose 查询?

javascript - Mongoose 从查询中检索特定的数组索引(不是完整的数组)

javascript - 我应该将所有数据库数据存储在内存中node.js吗?

linux - 为什么 node.js+mongodb 不为每秒发送 100 个请求提供 100 个请求/秒的吞吐量?

javascript - 可以使用纯文本文件进行身份验证吗?

c# - 在 MongoDB 中保存 POCO 时忽略一个属性,但在序列化为 JSON 时不忽略它

node.js - 使用 Mongo 排序

javascript - 使用socket.io(NodeJs)检测客户端是否与网络断开连接

node.js - mongoose document.validate 从 4.4x 更新到 ^4.5.1 后抛出 RangeError