Node.js Mongoose在一个查询中更新或创建多个值
我有这样一份文件:Node.js Mongoose在一个查询中更新或创建多个值,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有这样一份文件: { "_id" : ObjectId("565e906bc2209d91c4357b59"), "userEmail" : "abc@example.com", "subscription" : { "project1" : { "subscribed" : false }, "project2" : { "subscribed" : true
{
"_id" : ObjectId("565e906bc2209d91c4357b59"),
"userEmail" : "abc@example.com",
"subscription" : {
"project1" : {
"subscribed" : false
},
"project2" : {
"subscribed" : true
},
"project3" : {
"subscribed" : false
},
"project4" : {
"subscribed" : false
}
}
}
router.post('/subscribe', function(req, res, next) {
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
// stuck here on update query
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
{
userEmail: "abc@example.com",
subscription: ["project1", "project4"]
}
{
$set: {
"subscription.project1":{"subscribed" : true},
"subscription.project4":{"subscribed" : true}
}
我正在使用express进行我的post web服务调用,如下所示:
{
"_id" : ObjectId("565e906bc2209d91c4357b59"),
"userEmail" : "abc@example.com",
"subscription" : {
"project1" : {
"subscribed" : false
},
"project2" : {
"subscribed" : true
},
"project3" : {
"subscribed" : false
},
"project4" : {
"subscribed" : false
}
}
}
router.post('/subscribe', function(req, res, next) {
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
// stuck here on update query
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
{
userEmail: "abc@example.com",
subscription: ["project1", "project4"]
}
{
$set: {
"subscription.project1":{"subscribed" : true},
"subscription.project4":{"subscribed" : true}
}
我的req
包含如下数据:
{
"_id" : ObjectId("565e906bc2209d91c4357b59"),
"userEmail" : "abc@example.com",
"subscription" : {
"project1" : {
"subscribed" : false
},
"project2" : {
"subscribed" : true
},
"project3" : {
"subscribed" : false
},
"project4" : {
"subscribed" : false
}
}
}
router.post('/subscribe', function(req, res, next) {
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
// stuck here on update query
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
{
userEmail: "abc@example.com",
subscription: ["project1", "project4"]
}
{
$set: {
"subscription.project1":{"subscribed" : true},
"subscription.project4":{"subscribed" : true}
}
以下是我想在这次通话中执行的步骤:
abc@example.com
不存在,请使用userEmail创建一个新文档作为abc@example.com
subscription
对象中存在的project1
和project4
。如果没有,就创建这些subscription
中存在project1
和project4
,则将subscripted
更新为true
$set
运算符将字段的值替换为指定值。根据文件:
如果该字段不存在,$set
将添加具有指定值的新字段,前提是该新字段不违反类型约束。如果为不存在的字段指定虚线路径,$set将根据需要创建嵌入文档,以实现该字段的虚线路径
由于您的req.subscription
将是一个数组,因此您必须构建查询。看起来像这样:
{
"_id" : ObjectId("565e906bc2209d91c4357b59"),
"userEmail" : "abc@example.com",
"subscription" : {
"project1" : {
"subscribed" : false
},
"project2" : {
"subscribed" : true
},
"project3" : {
"subscribed" : false
},
"project4" : {
"subscribed" : false
}
}
}
router.post('/subscribe', function(req, res, next) {
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
// stuck here on update query
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
{
userEmail: "abc@example.com",
subscription: ["project1", "project4"]
}
{
$set: {
"subscription.project1":{"subscribed" : true},
"subscription.project4":{"subscribed" : true}
}
您可以使用从req.subscription=[“project1”,“project4”]数组创建对象
var subscription= req.subscription.reduce(function(o,v){
o["subscription." + v] = {subscription:true};
return o;
},{});
然后您的代码变成:
router.post('/subscribe', function(req, res, next) {
var subscription= req.subscription.reduce(function(o,v){
o["subscription." + v] = {subscription:true};
return o;
},{});
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
$set: subscription
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
维马尔拉
您可以使用。$set
运算符将字段的值替换为指定值。根据文件:
如果该字段不存在,$set
将添加具有指定值的新字段,前提是该新字段不违反类型约束。如果为不存在的字段指定虚线路径,$set将根据需要创建嵌入文档,以实现该字段的虚线路径
由于您的req.subscription
将是一个数组,因此您必须构建查询。看起来像这样:
{
"_id" : ObjectId("565e906bc2209d91c4357b59"),
"userEmail" : "abc@example.com",
"subscription" : {
"project1" : {
"subscribed" : false
},
"project2" : {
"subscribed" : true
},
"project3" : {
"subscribed" : false
},
"project4" : {
"subscribed" : false
}
}
}
router.post('/subscribe', function(req, res, next) {
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
// stuck here on update query
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
{
userEmail: "abc@example.com",
subscription: ["project1", "project4"]
}
{
$set: {
"subscription.project1":{"subscribed" : true},
"subscription.project4":{"subscribed" : true}
}
您可以使用从req.subscription=[“project1”,“project4”]数组创建对象
var subscription= req.subscription.reduce(function(o,v){
o["subscription." + v] = {subscription:true};
return o;
},{});
然后您的代码变成:
router.post('/subscribe', function(req, res, next) {
var subscription= req.subscription.reduce(function(o,v){
o["subscription." + v] = {subscription:true};
return o;
},{});
MyModel.findOneAndUpdate(
{
userEmail: req.body.userEmail
},
{
$set: subscription
},
{
upsert: true
}, function(err, raw) {
if (err) return handleError(err);
res.json({result: raw});
}
)
});
伟大的很高兴我能帮忙!伟大的很高兴我能帮忙!