使用 mongoose 更新数据库条目 [英] Update database entry using mongoose

查看:41
本文介绍了使用 mongoose 更新数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我用的是猫鼬.

我已经建立了这个查询来找到我想要的项目:

const projects = await ClientManagers.findOne({'project.contactPerson.work_email' : 'testing@email.com'} , { 'project.$.companyName': 1 });

这会从我的数据库中返回一个对象,如下所示:

<代码>{'项目名称':'x','公司名称':'x bv'}

如何将公司名称更新为Y bv"而不是x bv".

解决方案

假设这是您的文档结构,

<代码>{_id": ObjectId(5f2ae5a4b1549ac0460920dd"),项目名称":A",项目": [{公司名称":T1",联系人":{work_email":t1@gmail.com"}},{公司名称":T2",联系人":{work_email":t2@gmail.com"}}]}

单一更新updateOne()

如果您知道 email 将是唯一的并且想要更新单个文档,请使用 updateOne().

  • 首先是查询部分查找条件,发送电子邮件 t1@gmail.com
  • 第二部分是设置/更新部分,这里$用于数组,因为project是一个数组,将companyName更新为T1 Company

await ClientManagers.updateOne({ 'project.contactPerson.work_email': 't1@gmail.com' },{$set: { "project.$.companyName": "T1 Company";}})

多次更新updateMany()

如果 email 不是唯一的并且想要在任何地方更新,那么使用 updateMany(),它将更新每个匹配的文档.

await ClientManagers.updateMany({ 'project.contactPerson.work_email': 't1@gmail.com' },{$set: { "project.$.companyName": "T1 Company";}})

<块引用>

不建议 update()要使用的方法,因为它在 mongoose 中已弃用,并且会给出 弃用警告,这个函数被替换为updateOne()updateMany()replaceOne()方法.

Hello i am using mongoose.

I have built this query that finds my desired project :

const projects = await ClientManagers.findOne({'project.contactPerson.work_email' : 'testing@email.com'} , { 'project.$.companyName': 1 });

this returns an object from my database like this :

{
 'projectName' : 'x',
  'companyName' : 'x bv'
}

How can i update the company name to be 'Y bv' instead of 'x bv'.

解决方案

Assuming this is your document structure,

{
    "_id" : ObjectId("5f2ae5a4b1549ac0460920dd"),
    "projectName" : "A",
    "project" : [ 
        {
            "companyName" : "T1",
            "contactPerson" : {
                "work_email" : "t1@gmail.com"
            }
        }, 
        {
            "companyName" : "T2",
            "contactPerson" : {
                "work_email" : "t2@gmail.com"
            }
        }
    ]
}

Single Update updateOne()

If you know email will be unique and want to update single document then use updateOne().

  • first is query part to find condition, email t1@gmail.com
  • second is set/update part, here $ is for array because project is an array, update companyName to T1 Company

await ClientManagers.updateOne(
    { 'project.contactPerson.work_email': 't1@gmail.com' },
    {
        $set: { "project.$.companyName": "T1 Companmy" }
    }
)

Multiple Update updateMany()

If email is not unique and want to update everywhere then use updateMany(), it will update every matching documents.

await ClientManagers.updateMany(
    { 'project.contactPerson.work_email': 't1@gmail.com' },
    {
        $set: { "project.$.companyName": "T1 Company" }
    }
)

Not suggesting update() method to use, because its deprecated in mongoose and will give Deprecation Warnings , this function is replaced with updateOne(), updateMany() and replaceOne() methods.

这篇关于使用 mongoose 更新数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆