我要做的是为博客帖子添加一个新的{field:value}.例如,如果我想开始跟踪网站上的展示次数.blog_posts.url:'http://www.example.com/01.html'如何为该博客帖子添加该展示次数属性?
我目前的文件结构:
{
email_address: 'webmaster@example.com',
password: 'random_password',
first_name: 'John',
last_name: 'Doe',
user_type: 'WEBMASTER',
newsletter: 'NO',
websites: [{
main_title: 'My Blog Website',
main_url: 'http://www.example.com',
blog_posts: [{
url: 'http://www.example.com/01.html',
title:'My first blog post',
description: 'My first description.'
}, {
url: 'http://www.example.com/02.html',
title: 'My second blog post',
description: 'My second description.'
}, {
url: 'http://www.example.com/03.html',
title: 'My third blog post',
description: 'My third description.'
}, {
url: 'http://www.example.com/04.html',
title: 'My fourth blog post',
description: 'My fourth description.'
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
这是我认为使用更新和制作upup TRUE的工作方式.
db.my_collection.update({'sites.blog_posts.url':'http://www.example.com/01.html'},{'$ set':{'websites.blog_posts.impressions':549}} ,真的)
我收到的错误是: *无法使用字符串字段名称附加到数组[blog_posts]*
也许"$ set"对此不正确,或者我不能用点符号来深入引用?我昨天刚开始使用MongoDB,任何帮助都会很棒.
谢谢!
鉴于您的架构,您尝试做的事情是不可能的.点符号可以是多级的,但如果有多个级别是数组,则不能再使用位置运算符'$'来寻址.
你需要这样做:
db.my_collection.update(
{'websites.blog_posts.url': 'http://www.example.com/01.html' },
{'$set': {'websites.$.blog_posts.$.impressions': 549}},
true );
Run Code Online (Sandbox Code Playgroud)
但是在更新中有两个位置运算符是不可能的,因为MongoDB只能确定第一个数组中元素的位置.
您唯一的选择是重新设计您的架构,以拥有一个专门的用户网站集合(在这种情况下,其他原因也更好).
| 归档时间: |
|
| 查看次数: |
8291 次 |
| 最近记录: |