使用 MongoDB 更新数组字段内的特定键/值 [英] Updating a specific key/value inside of an array field with MongoDB

查看:42
本文介绍了使用 MongoDB 更新数组字段内的特定键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为序言,我已经使用 MongoDB 大约一个星期了,所以这可能是一个非常简单的答案.

As a preface, I've been working with MongoDB for about a week now, so this may turn out to be a pretty simple answer.

我的集合中已经存储了数据,我们将称这个集合为 content,因为它包含文章、新闻等.这些 articles 中的每一个都包含另一个称为author 包含作者的所有信息(地址、电话、职务等).

I have data already stored in my collection, we will call this collection content, as it contains articles, news, etc. Each of these articles contains another array called author which has all of the author's information (Address, Phone, Title, etc).

目标 - 我正在尝试创建一个查询,该查询将更新特定作者所在的每篇文章的作者地址,并且仅更新指定的作者块(而不是数组中存在的其他作者)).

The Goal - I am trying to create a query that will update the author's address on every article that the specific author exists in, and only the specified author block (not others that exist within the array).

某种特定文章的全局更新",会影响他/她对存在的每条内容的信息.

Sort of a "Global Update" to a specific article that affects his/her information on every piece of content that exists.

以下是带有 authorcontent 的示例.

Here is an example of what the content with the author looks like.

{ 
"_id" : ObjectId("4c1a5a948ead0e4d09010000"), 
"authors" : [
    {
        "user_id" : null,
        "slug" : "joe-somebody",
        "display_name" : "Joe Somebody",
        "display_title" : "Contributing Writer",
        "display_company_name" : null,
        "email" : null,
        "phone" : null,
        "fax" : null,
        "address" : null,
        "address2" : null,
        "city" : null,
        "state" : null,
        "zip" : null,
        "country" : null,
        "image" : null,
        "url" : null,
        "blurb" : null
    },
    {
        "user_id" : null,
        "slug" : "jane-somebody",
        "display_name" : "Jane Somebody",
        "display_title" : "Editor",
        "display_company_name" : null,
        "email" : null,
        "phone" : null,
        "fax" : null,
        "address" : null,
        "address2" : null,
        "city" : null,
        "state" : null,
        "zip" : null,
        "country" : null,
        "image" : null,
        "url" : null,
        "blurb" : null
    },
], 
"tags" : [ 
    "tag1", 
    "tag2", 
    "tag3" 
], 
"title" : "Title of the Article" 
}

我可以通过运行以下命令找到该作者创建的每篇文章:

I can find every article that this author has created by running the following command:

db.content.find({authors: {$elemMatch: {slug: 'joe-somebody'}}});

所以理论上我应该能够更新 slug joe-somebody 但不是 jane-somebody(第二作者)的 authors 记录),我只是不确定您究竟是如何访问和更新该作者的每条记录的.

So theoretically I should be able to update the authors record for the slug joe-somebody but not jane-somebody (the 2nd author), I am just unsure exactly how you reach in and update every record for that author.

我认为我走在正确的轨道上,这是我尝试过的.

I thought I was on the right track, and here's what I've tried.

b.content.update(
   {authors: 
     {$elemMatch: 
       {slug: 'joe-somebody'}
     }
   },
   {$set: 
     {address: '1234 Avenue Rd.'}
   },
   false,
   true
);

我只是相信 $set 语句中缺少某些内容来指定正确的作者并指向正确的数组内部.有什么想法吗?

I just believe there's something I am missing in the $set statement to specify the correct author and point inside of the correct array. Any ideas?

**更新**

我现在也试过了:

b.content.update(
   {authors: 
     {$elemMatch: 
       {slug: 'joe-somebody'}
     }
   },
   {$set: 
     {'authors.$.address': '1234 Avenue Rd.'}
   },
   false,
   true
);

解决方案:

这终于对我有用了!

Solution:

This is what finally worked for me!

db.content.update({'authors.slug':'joe-somebody'},{$set:{'authors.$.address':'Address That I wanted'}},false,true);

它正确更新了所有记录,谢谢!

It updates all the records properly, thanks!

推荐答案

也许你可以使用 $ 操作符 (positional-operator)?

Maybe you can use the $ operator (positional-operator)?

这篇关于使用 MongoDB 更新数组字段内的特定键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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