我有“项目”集合如下。我想更新“用户”和“所有者”中的电子邮件域为“@ mailm365.com“
{
"_id" : ObjectId("637e0af92ce49a1a816807bc"),
"title" : "Project-1",
"description" : "Project-1 Description",
"users" : [
{
"_id" : ObjectId("5f8881823542b1b499d7b351"),
"fullname" : "User-1",
"email" : "user-1@mail.com"
}
],
"owners" : [
{
"_id" : ObjectId("5f8881823542b1b499d7b351"),
"fullname" : "User-2",
"email" : "user-2@mail.com"
},
{
"_id" : ObjectId("61fb71ca294d34537dafe4c9"),
"fullname" : "User-3",
"email" : "user-3@mail.com"
}
]}
我已经尝试了以下查询,但没有成功。有任何帮助使此更新查询工作吗?
db.Project.find().forEach(function(project)
{
project.users.forEach(function(user){
var updatedDomain = user.email.replace('@mail.com', '@mailm365.com');
user.email = updatedDomain;
});
project.owners.forEach(function(owner){
var updatedDomain = owner.email.replace('@mail.com', '@mailm365.com');
owner.email = updatedDomain;
});
db.Project.updateOne(
{
_id:project._id
},
{
$set:
{
"users": project.users,
"owners": project.owners
}
}
);
});
1条答案
按热度按时间kxeu7u2r1#
查询
$split
函数,该函数是管道运算符$map
,$this
将成为文档成员,$mergeObjects
将成为新的电子邮件成员@mailm365.com
新docmain名称连接$cond
来只在特定的PLaymongo