Javascript mongodb只更新特定的子元素
我的数据库结构如下所示:Javascript mongodb只更新特定的子元素,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,我的数据库结构如下所示: { "_id" : ObjectId("51e66873f6a6600436000001") ,"email" : "asd@asd.de", ,"attribute_group_a" : { "attribute_a" : "" ,"attribute_b" : "" ,"attribute_c" : "" ,"attribute_d" : "" }, ,"attribute_group_b" : {
{
"_id" : ObjectId("51e66873f6a6600436000001")
,"email" : "asd@asd.de",
,"attribute_group_a" : {
"attribute_a" : ""
,"attribute_b" : ""
,"attribute_c" : ""
,"attribute_d" : ""
},
,"attribute_group_b" : {
"attribute_subgroup_b_a" : {
"attribute_a" : ""
,"attribute_b" : ""
,"attribute_c" : ""
,"attribute_d" : ""
}
,"attribute_subgroup_b_b" : {
"attribute_a" : ""
,"attribute_b" : ""
,"attribute_c" : ""
,"attribute_d" : ""
}
}
}
{
attribute_group_b:
{
attribute_subgroupgroup_b_a:
{
att_a: "xy"
,att_b: "xy"
}
}
}
假设我想更新att_subgrp_b_a:
exports.updateProfil = function(req, res, item, callback) {
var email = req.session.email;
db.collection('profiles', function(err, collection) {
collection.update({"email": email},{$set: item}, function(err, result)
变量“item”如下所示:
{
"_id" : ObjectId("51e66873f6a6600436000001")
,"email" : "asd@asd.de",
,"attribute_group_a" : {
"attribute_a" : ""
,"attribute_b" : ""
,"attribute_c" : ""
,"attribute_d" : ""
},
,"attribute_group_b" : {
"attribute_subgroup_b_a" : {
"attribute_a" : ""
,"attribute_b" : ""
,"attribute_c" : ""
,"attribute_d" : ""
}
,"attribute_subgroup_b_b" : {
"attribute_a" : ""
,"attribute_b" : ""
,"attribute_c" : ""
,"attribute_d" : ""
}
}
}
{
attribute_group_b:
{
attribute_subgroupgroup_b_a:
{
att_a: "xy"
,att_b: "xy"
}
}
}
当我现在更新文件=>时,它会删除attr_group_b中的所有内容,并将其替换为“item”
这意味着attr_subgrp_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_b_
我希望它在“item”中查找属性,将它们替换为db,让所有其他obj保持不变尝试下面的查询
var email='emailid';
var item='whichuwanttoupdate';
collection.update(
{"email": email},
{$set:{'attribute_group_b.attribute_subgroup_b_a':item}},
function(err,result){
});
尝试下面的查询
var email='emailid';
var item='whichuwanttoupdate';
collection.update(
{"email": email},
{$set:{'attribute_group_b.attribute_subgroup_b_a':item}},
function(err,result){
});
@Pika上述代码应该有效…见此文档@Pika上述代码应该有效…见此文档