在mongoDB中的$投影findOneAndUpdate() [英] $ projection in mongoDB findOneAndUpdate()
问题描述
我正在尝试使用express和mongoose建立一个简单的任务队列.这个想法是获得一个客户,并返回广告系列ID和客户ID(这是Campaign的子文档).每次有人获取客户时,其状态代码都设置为1.我提出了以下查询:
I'm trying to build a simple task queue with express and mongoose. The idea is acquire a single client and return campaign id and client id (which is a subdocument of campaign). Each time someone acquires a client, its status code is set to 1. I've come up with the following query:
router.post('/lease', (err, res) => {
Campaign.findOneAndUpdate({'isEnabled': true, 'clients.contact_status_code': 0}, {
'$set': { 'clients.$.contact_status_code': 1 },
},
{
new: true,
projection: {
'clients.$': true,
},
},
(err, campaign) => {
if (err) {
return res.send(err);
}
res.json(campaign);
}
);
});
但是连接到此端点后,我所得到的就是:
But all i'm getting after connecting to this endpoint is this:
{"_id":"591483241a84946a79626aef","clients":[{},{}]}
在我看来,问题在于$投影,但我不知道如何解决此问题.
It seems to me that the problem is with the $ projection, but I have no idea how to fix this.
我尝试使用以下代码,利用$ elemMatch:
I tried using the following code, utilizing $elemMatch:
router.post('/lease', (err, res) => {
Campaign.findOneAndUpdate({'isEnabled': true, 'clients.contact_status_code': 0}, {
'$set': { 'clients.$.contact_status_code': 1 },
},
{
new: true,
projection: {
clients: {
'$elemMatch': {contact_status_code: 1},
}
},
},
(err, campaign) => {
if (err) {
return res.send(err);
}
res.json(campaign);
}
);
});
不幸的是,每个请求都产生了集合中符合条件的第一个子文档,而不是特定于已更新的子文档.这是一个示例:
Unfortunately, each request yields the first subdocument in the collection, that matched the criteria -- not specifically the one that was updated. Here is an example:
说,我在mongo中有以下文件:
Say, i have the following document in mongo:
{
"_id" : ObjectId("591493d95d48e2738b0d4317"),
"name" : "asd",
"template" : "{{displayname}}",
"isEnabled" : true,
"clients" : [
{
"displayname" : "test",
"_id" : ObjectId("591493d95d48e2738b0d4319"),
"contact_status_code" : 0
},
{
"displayname" : "client",
"_id" : ObjectId("591493d95d48e2738b0d4318"),
"contact_status_code" : 0
}
],
"__v" : 0
}
我第一次运行查询并获得以下结果:
I run the query for the first time and get the following result:
{"_id":"591493d95d48e2738b0d4317","clients":[{"displayname":"test","_id":"591493d95d48e2738b0d4319","contact_status_code":1}]}
通知客户端ID"591493d95d48e2738b0d4319"-这次它按预期运行.但是,当我第二次运行相同的查询时,尽管我希望得到一个ID为"591493d95d48e2738b0d4318"的对象,但我得到的对象完全相同.
Notice client id "591493d95d48e2738b0d4319" -- this time it runs as expected. But when i run the same query the second time, I get absolutely the same object, although I expect to get one with id "591493d95d48e2738b0d4318".
推荐答案
问题出在new: true
这是一个有效的示例:
Campaign.findOneAndUpdate({'isEnabled': true, 'clients.contact_status_code': 0}, {
'$set': { 'clients.$.contact_status_code': 1 },
},
{
//new: true <-- this was causing the trouble
projection: {
clients: {
'$elemMatch': {contact_status_code: 0}, // 0 because the old record gets matched
},
},
},
(err, campaign) => {
if (err) {
return res.send(err);
}
res.json(campaign);
}
);
我假设,当设置了new:true
时,mongo会丢失匹配的上下文.不幸的是,这种方法返回了旧记录,但这仍然可以满足我获取_id的需求.
I assume, when the new:true
is set, mongo loses the matching context. This approach returns the old record, unfortunately, but that still serves my needs to get the _id.
这篇关于在mongoDB中的$投影findOneAndUpdate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!