mongodb - Using $set to update a document's value with key containing a dot(.), MongoDB
问题描述
I have this very basic collection (blog.posts
) from which I want to update the value of a key (author.name
) using the $set
modifier. Here is a sample of a document from the collection:
{
"_id" : ObjectId("5ca66e7d8a3bb6e58e0d85f4"),
"s_id": "unique_post",
"title" : "MongoDB is cool",
"author.name" : "Terence"
}
I want to change the value of the author.name
key from Terence to Benjamin and this is the query with which I'm trying to acheive the result:
db.blog.posts.update({"s_id": "unique_post"}, {"$set":{"author.name":"Benjamin"}})
The issue is that when I execute this query, I end up with a new nested ducument as shown:
{
"_id" : ObjectId("5ca66e7d8a3bb6e58e0d85f4"),
"title" : "MongoDB is cool",
"author.name" : "Terence",
"author" : {
"name" : "Benjamin"
}
}
I know that this is to be expected but my question is: How do I modify a value in a document who's key contains a dot(.) ?
Please I'm new to mongoDB and any other tips would be highly appreciated.
解决方案
As per MongoDB documentation dot(.) is a restricted character in field names. So you cannot have dot(.) in field names. That is the reason it converts the dot(.) value into subdocument.
推荐阅读
- wso2 - WSO2 API Manager 安装:Apt install 无法找到包 wso2am-3.2.0
- spring-boot - 如何使用 Spring Security 5.1+ 启用 Auth0 OIDC 登录
- mysql - 在 clojure 中使用 korma 在 mysql 中插入多行
- azure - 如果安装了 Microsoft.Azure.ServiceBus Nuget 包,Application Insights 将不起作用
- java - 无法在房间数据库 Android 中插入值
- sql - VBA:sql,检索多个值
- javascript - 不理解错误“未捕获的类型错误:”
- django - Factory Boy:如何根据参数设置条件 SubFactory() 属性
- c# - C# SQL - 搜索地址,如果没有找到插入地址与数据库中已知的街道记录
- processing - 如何使用 controlP5 中的制表键从一个控件切换到另一个控件?