[英]MongoDB Nested Documents Aggregation using Change Events
The problem is that when I push a new document inside the array field it becomes a dotted notation.问题是当我在数组字段中推送一个新文档时,它变成了一个点符号。
What I am trying to achieve is to filter the the response of the Change Event or the Collection.watch().
我想要实现的是过滤更改事件或 Collection.watch() 的响应。
But this happens
但这会发生
There is no way that I can access the updateDescription.updatedFields.message because it became a dot notation.
我无法访问 updateDescription.updatedFields.message 因为它变成了点符号。
I tried to use我试着用
var filter = [{
$match: {
$and: [
{ 'documentKey._id': req.body.id },
{'updateDescription.updatedFields.message': // value of current length of the messages array}
]
}
}];
and和
var filter = [{
$match: {
$and: [
{ 'documentKey._id': req.body.id },
{'updateDescription.updatedFields.messages' : {$exists: true}}
]
}
}];
I also used projection:我还使用了投影:
try{
res.status(200).set({
"connection":"keep-alive",
"cache-control": "no-cache",
"content-type":"application/json"
});
var query = await User.findOne({'_id': req.body.id});
var len = parseInt(query['messages'].length);
var _str = 'messages.'+String(len);
console.log(_str)
var project = [{$project: {'updateDescription.updatedFields' : {$objectToArray: '$updateDescription.updatedFields'}}}]
var filter = [{
$match: {
$and: [
{ 'documentKey._id': req.body.id },
{'updateDescription.updatedFields.k': _str}
]
}
}];
await User.watch(filter, project).on('change', data => console.log(data))
}catch(err){
res.status(500).send(err);
}
But still I haven't got any solution how to solve this.但我仍然没有任何解决方案如何解决这个问题。
The problem is that you are projecting it wrong.问题是你投射错了。
$project: {'updateDescription.updatedFields' : {$objectToArray: '$updateDescription.updatedFields'}}
is should be应该是
{$project: { 'updateDescription.updatedFields' : {$objectToArray: '$updateDescription.updatedFields'}, 'documentKey._id': req.body.id }}
Then you go use regex on the $match然后你 go 在 $match 上使用正则表达式
{$match: {
$and: [
{ 'documentKey._id': req.body.id },
{'updateDescription.updatedFields.k': {$regex:"^matches"}}
]}
Here's the full working snippet:这是完整的工作片段:
try{
res.status(200).set({
"connection":"keep-alive",
"cache-control": "no-cache",
"content-type":"application/json"
});
var filter = [
{$project: { 'updateDescription.updatedFields' : {$objectToArray: '$updateDescription.updatedFields'}, 'documentKey._id': req.body.id }},
{$match: {
$and: [
{ 'documentKey._id': req.body.id },
{'updateDescription.updatedFields.k': {$regex:"^matches"}}
]
}
}];
await User.watch(filter).on('change', data => console.log(data.updateDescription.updatedFields))
}catch(err){
res.status(500).send(err);
}
The reason why it's not matching is because you are using $and operator and there's no "documentKey._id" to match with since you projected it wrong.它不匹配的原因是因为您使用的是 $and 运算符并且没有“documentKey._id”可以匹配,因为您将其投影错误。
The $filter
and $project
stages should be together in a single array. $filter
和$project
阶段应该放在一个数组中。 The watch
function takes (Pipeline Array, Options Object). watch
function 采用(管道阵列,选项对象)。 Try:尝试:
var filter = [
{$project: {'updateDescription.updatedFields' : {$objectToArray: '$updateDescription.updatedFields'}}},
{$match: {
$and: [
{ 'documentKey._id': req.body.id },
{'updateDescription.updatedFields.k': _str}
]
}
}];
await User.watch(filter).on('change', data => console.log(data))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.