[英]How to update a subdocument inside another subdocument in mongoose using atomic query?
My Mongoose schema looks as follows: 我的猫鼬模式如下所示:
var MousePos = Schema({
x: { type: Number},
y: { type: Number},
clickCount: {type: Number, default:0}
});
var MouseTrackInfo = Schema({
pos:{ type: [MousePos], default:[]},
element: {type: String, index:true},
clickCount:{ type: Number, default: 0 },
timeSpent: { type: Number, default: 0 }
});
var MouseTrackSchema = Schema({
pageUrl: { type: String},
applicationId: { type: String, index: true },
mouseTrackLog: { type: [MouseTrackInfo], default:[]},
urlId: {type: String, index: true}
});
Now what i want to do is to update clickCount in pos subdocument when element, x and y values are specified. 现在,我要执行的操作是在指定元素x和y值时更新pos子文档中的clickCount。
I tried a number of approaches, but nothings seems to be working. 我尝试了多种方法,但似乎无济于事。 Moreover i can't use two $ operator in my query too.
此外,我也不能在查询中使用两个$运算符。 Suggestions will be welcome.
欢迎提出建议。 I want to make this query as atomic as possible.
我想使此查询尽可能原子。
I was not able to figure out a reliable way so i changed my schema to following: 我无法找出一种可靠的方法,因此我将架构更改为以下内容:
var MouseTrackInfo = Schema({
element: {type: String, index:true},
clickCount:{ type: Number, default: 0 },
timeSpent: { type: Number, default: 0 },
x: { type: Number, default:0},
y: { type: Number, default: 0},
identifier: {type: String}
});
var MouseTrackSchema = Schema({
pageUrl: { type: String},
applicationId: { type: String, index: true },
mouseTrackLog: { type: [MouseTrackInfo], default:[]},
urlId: {type: String, index: true}
});
MouseTrackSchema.methods.incrementClickCount = function(element,pageX,pageY,cb) {
var parent = this;
this.model('MouseTrackModel').update(
{
'applicationId':this.applicationId,
'urlId':this.urlId,
'mouseTrackLog.identifier': element+"X"+pageX+"Y"+pageY
},
{
'$inc':{'mouseTrackLog.$.clickCount':1}
},
{
'new': true
},
cb);
}
This allowed my execute a atomic query. 这使我可以执行原子查询。 Any other solution are welcome.
任何其他解决方案都欢迎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.